Getting the same output of dir() with selected files as with selecting a folder.
5 views (last 30 days)
Show older comments
Amanda Beatty
on 16 Mar 2020
Commented: Ameer Hamza
on 16 Mar 2020
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?
0 Comments
Accepted Answer
Ameer Hamza
on 16 Mar 2020
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 Comments
Ameer Hamza
on 16 Mar 2020
Can you paste your updated code here. I think you didn't remove this line
fnames2 = char(fnames2);
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!