Getting the same output of dir() with selected files as with selecting a folder.

I'm working with some inherited code that pulls in a bunch of .csv files and analyzes them. For it to work you have to place the .m file in the same folder where the .csv files are located, and then it searches for all the .csv files and pulls them in. It creates the variable "fnames" and the rest of the code is based off that variable. That portion of the code is here:
currentDir = pwd;
currentDir = strcat(pwd,'\');
currentDirFiles = strcat(currentDir,'*.csv');
fnames = dir(currentDirFiles);
For revision control, I'm trying to make it so the .m file can always be in a certain location, and then you can navigate to where the .csv files are and select the files you want to include in the analysis. I don't want it to just select all the .csv files in the folder, because every once in a while there is a random unrelated .csv file in there that I don't want included. This is what I have tried
[fnames2, selectedDir] = uigetfile('*.csv', 'Select One or More Files', ...
'C:\Documents',...
'MultiSelect', 'on');
fnames2=fnames2'; %make vertical
fnames2 = char(fnames2); %turn into character
fnames2 = strcat(selectedDir, fnames2); %concatenate file path with file names
fnames = dir(fnames2);
I am just trying to get an identical variable "fnames" that can be used for the rest of the code, but when I use dir() for specifically selected files rather than a whole folder, it doesn't give all the same information. How do I get all the same information as the first version of fnames?

 Risposta accettata

Try this
[fnames2, selectedDir] = uigetfile('*.csv', 'Select One or More Files', ...
'C:\Documents',...
'MultiSelect', 'on');
fnames2 = fnames2'; %make vertical
fnames2 = strcat(selectedDir, fnames2);
for i=1:numel(fnames2)
fnames(i) = dir(fnames2{i});
end
Remember to remove the line
fnames2 = char(fnames2); %turn into character

4 Commenti

Error: Brace indexing is not supported for variables of this type.
fnames2 is a 2x62 char. And when I change that line to :
for i=1:numel(fnames2)
fnames(i) = dir(fnames2(i));
end
I get the error: Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
OH my gosh. I forgot to remove the line that you specifically said to remove. That worked. Thank you!
Can you paste your updated code here. I think you didn't remove this line
fnames2 = char(fnames2);
No problem. Glad to be of help.

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by