-2

As Shai and CTZStef suggested, when I have to open multiple files with similar names in MATLAB, I have to do

for k=1:size(fls)
    fileName = strcat('int_',num2str(k),'.ASC');
    A(k) = importdata(fileName, ' ', 9);
    x(k) = A(k).data(:,1);
    y(k) = A(k).data(:,2);
end

or also

fls = dir('int_00_*.ASC');
for fi=1:numel(fls)
    A(fi) = importdata(fls(fi).name, ' ',9);
end

Well, the problem is that none of them works. What should I do? Is it a problem with my MATLAB version?

Community
  • 1
  • 1
ragnar
  • 57
  • 1
  • 4
  • 10
  • Please state what the "none of them works". Looking at your other questions, I would say you have problems constructing filenames. – Dedek Mraz Mar 13 '13 at 09:10
  • @Dedek Mraz if I declare the filename with fls or strcat, ie I do fls = dir(...) and then I write fls(2) or fls(56), that's all right: it's the right filename. The problem is that importdata doesn't "like" opening the files in this way – ragnar Mar 13 '13 at 09:14
  • Again, I don't know what you are trying to load, but putting it into a field in an array looks wrong. Maybe this is the problem? Did you try assigning it to `A{fi}`? – Dedek Mraz Mar 13 '13 at 09:18
  • @Dedek Mraz Usually when you open a file with importdata you do: importdata(filename, ' ', #lines). This works as long as you have one file only. What I am wondering is: how can you do when you have many files to open and they all have similar names? People suggested putting the files in a loop, but it doesn't work – ragnar Mar 13 '13 at 09:21
  • @Shai yes, as I am new to this forum I don't know how it works... should I rather have kept the previous question open? – ragnar Mar 13 '13 at 09:25
  • @ragnar: what I'm saying is that you assign an array from the file to a single field in array `A`. I'm assuming you have multiple values in one file. This will not work. Again: have you tried with `A{fi}`? – Dedek Mraz Mar 13 '13 at 09:27
  • @Shai OK, I'll bear it in mind for the future – ragnar Mar 13 '13 at 09:28
  • @DedekMraz yes, and it doesn't work. But maybe Shai has suggested the solution I should have followed and I didn't because I didn't read it properly – ragnar Mar 13 '13 at 09:31

1 Answers1

2

You need to follow the answers you got more closely:

  1. The strcat solution CANNOT handle the zero padding of the file names. You'll ahve to manually rename all files from 'int_001.ASC' to 'int_1.ASC'.
    UPDATE (due to @DedekMraz's comment): you'll need to modify the strcat input string to strcat('int_', num2str( k, '%03d' ), '.ASC');

  2. You can use a strategy similar to this one. See the update to the answer you already got.

  3. The input you gave your dir function is wrong it has to be dir('int_*.ASC') and not dir('int_00_*.ASC').

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    For padding you can just use `num2str(k,'%03d')`. I think this should pad the integer with leading zeros – Dedek Mraz Mar 13 '13 at 09:24
  • @Shai OK, I'll try to be more specific. The biggest problem is that I'd have to manually change the names of 256 files that are something like int_00_091_000.ASC, with only 000 being fixed. The other two digit series vary in different ways and I must keep them just as they are because they are important to know what's inside the files. Having said that I have noticed the error message is always "error using importdata: unable to open file". Therefore the files are properly "marked" (if I type fls(34) I got int_01_092_000.ASC: all right). The problem is with importdata. I'll try with sprintf – ragnar Mar 13 '13 at 09:56
  • @ragnar - please see the update regarding zero padding in `str2num` (READ UPDATES TO ANSWERS). try using `importdata` with full file name (including full path). You might want to look at `fullfile` command for that. – Shai Mar 13 '13 at 10:01