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

3 次查看(过去 30 天)
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?

采纳的回答

Ameer Hamza
Ameer Hamza 2020-3-16
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by