필터 지우기
필터 지우기

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
Ameer Hamza
Ameer Hamza 2020년 3월 16일
Can you paste your updated code here. I think you didn't remove this line
fnames2 = char(fnames2);

댓글을 달려면 로그인하십시오.

추가 답변 (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