-1

I am working with this routine below that performs the specific M routine for all listed ascii files that I list as follows:

files={'file1name.asc', 'file2name.asc', 'fileNname.asc'};

ratios=NaN*zeros(1,length(files));
for i=1:length(files)
    ratios(i)=specific_m_routine(files{i}); 
end

How do I simply change this to call all .asc files in the directory, rather than listing each fileNname.asc? Thanks!

Svet
  • 11
  • 5
  • `D = dir('*.asc')` would return a structure, in which you can access the name through `D(SomeIndex).name`. Is this what you mean? – Benoit_11 Jan 22 '16 at 18:05
  • Uh, I'm not sure... What do you mean by, SomeIndex? I just want it to process the data stored in the ascii files in the directory, regardless of their name. I'm looking for a way to avoid tediously typing each file in the directory by name, as the code requires currently. – Svet Jan 22 '16 at 18:09

1 Answers1

0

I'm assuming you want to pass a filename to your specific_m_routine function? If so, you need to use dir and files(k).name rather than listing all of your files in a cell array and referring to them with files{k}.

files = dir('*.asc');

ratios = nan(1, numel(files));

for k = 1:numel(files)
    ratios(k) = specific_m_routine(files(k).name); 
end

Or if you want to be able to use any directory.

folder = fullfile('path', 'to', 'data');

files = dir(fullfile(folder, '*.asc'));

ratios = nan(1, numel(files));

for k = 1:numel(files)
    ratios(k) = specific_m_routine(fullfile(folder, files(k).name));
end

This way will not require the files you're trying to load to be in the current directory.

As a side note, I feel like you have asked some variant of this exact question the past few days but haven't taken the time to digest the answers you have gotten. Please go back and review all of them to get a better idea of what you're trying to do.

Suever
  • 64,497
  • 14
  • 82
  • 101
  • That worked, thanks! I actually started a different routine than the one from the last few days. – Svet Jan 22 '16 at 20:51
  • @Svet There is a decent amount of overlap between projects, so take some time and study these sorts of things because you'll see them again! – Suever Jan 22 '16 at 20:52