How do I import file names that change in a loop?
조회 수: 4 (최근 30일)
이전 댓글 표시
I need to import Trial01, Trial02 and so on then Trial10, Triall11 but how do I define the 0 in the first 9 trails from 1 to 9 then not from 10 to 54?
This is my code to import the data:
%% Import data
numfiles = 54; % number of excel files mydata=cell(numfiles,1); % defining size of mydata d=dir('Trial*.csv');
for i=7:length(mydata) % loop to import mutliple excel files
try
mydata{i} = xlsread(d(i).name); % import files into mydata
catch
disp([d(i).name 'read failed'])
end
myfilename = sprintf('Trial%i.csv', i); % define file name
mydata{i} = xlsread(myfilename); % import files into mydata
댓글 수: 1
dpb
2014년 5월 12일
편집: dpb
2014년 5월 13일
Again, I suggest just modify your present use of dir slightly instead.
It's well to know the format string to generate the place-holding 0 in a numeric field, and there are times when it's about the only choice but here there's still a very convenient wildcard pattern that works and it's still more work to generate names for existing files and you then have the problem of there may not be every one extant and you're back to a similar problem as that you have in the other thread of skipping empty content.
It does sometimes take a little creativity to get the right wild card--in this case,
d=[dir('Trial0?.csv');dir('Trial1?.csv')];
will return the values you want by concatenating the two directory structures for those from 00 thru 09 and 10-19, respectively. You can also get more exotic using post processing to eliminate those you don't want from a full dir() as an example is given in the doc's.
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!