How do I import file names that change in a loop?
    8 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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 Commento
  dpb
      
      
 il 12 Mag 2014
				
      Modificato: dpb
      
      
 il 13 Mag 2014
  
			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.
Risposta accettata
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su File Operations in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


