Question about loading multiple files

Greetings,
I wrote an 'm' function to load several files recursively and do some processing/graphing.
If I declare the file names in a string array, then the code executes with no issue.
However, if I load all files with the dir() function, I get an error message after the 1st file is loaded. What am I missing?
DIR_STR=dir('*.mf4');
FILES= DIR_STR.name
for ii=1:5
FILE=FILES(ii,:)
disp(['ii = ' num2str(ii) ' -- File: ' FILE])
end

回答(2 个)

Stephen23
Stephen23 about 9 hours 前
编辑:Stephen23 about 9 hours 前
"What am I missing?"
You are missing the fact that this line
FILES= DIR_STR.name
defines a comma-separated list:
from which you only request the first element from DIR_STR, and you discard all the rest. Thus your code will not work as it is written (there are other bugs too, but that is the one you asked about in your question). The recommended (much more robust, more versatile) approach is to use indexing to directly access the elements of the structure returned by DIR:
S = dir('*.mf4');
for ii = 1:numel(S)
F = S(k).name;
fprintf('ii = %d -- File: %s\n',ii,F)
end
Because then you can easily start to write much better code, e.g. by not storing your datafiles in the same location as your code files:
P = '.'; % absolute/relative path to where the files are saved
S = dir(fullfile(P,'*.mf4'));
for ii = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
fprintf('ii = %d -- File: %s\n',ii,F)
end
See also:

4 个评论

Alternatively:
DIR_STR = dir('*.mf4');
FILES = {DIR_STR.name}; % Create a cell array
for ii = 1:5
FILE = FILES{ii};
disp(['ii = ', num2str(ii), ' -- File: ', FILE]);
end
Using absolute file names is a safe method.
I'd prefer Stephen's version, because it is cleaner to use the struct array replied by dir directly. Creating an additional cell array is an indirection.
@Stephen23. Thank You. Can you kindly clarify what you mean by 'other bugs' ?
"Can you kindly clarify what you mean by 'other bugs' ?"
1) not using absolute/relative filenames.
2) looping over a hard-coded 1 to 5 without confirming that five (or more) files were found.
3) indexing into rows 2+ of a character row vector.
These will throw errors in certain very common circumstances, yet are easily avoided.
> 3) indexing into rows 2+ of a character row vector.
For your code to work, FILES would have to be a column string array (in which case you might as well index FILES(ii) instead of FILES(ii,:)
If FILES was a 2D or more string array, then FILES(ii,:) would be a nonscalar string, in which case the disp() would give odd results.
Alternately, your code would sort of work if FILES were a character array -- but if so then the individual file names would be left justified and blank padded on the right out to the common width. Blank padded file names would work for disp() purposes, but would typically fail for other purposes such as mdfRead() as mdfRead() would try to find the filename literally including the trailing blanks.

请先登录,再进行评论。

Djamil
Djamil about 2 hours 前
Thank You. Agree fully, though the snippet code I uploaded was much simplified to diagnose the main error (which you identified).
I tend to put a strong emphasis on file names to minimize some issues with Matlab ...
There was a time where I used Matlab daily for my work (signal/data processing) ... but veered away as there were more 'dedicated packages' (e.g Head Acoustics). Now I see Matlab has evolved quite a bit ... and I need to invest more time into it.
Thanks for all the help. Cheers

类别

帮助中心File Exchange 中查找有关 File Operations 的更多信息

产品

版本

R2025b

提问:

2026-3-3,12:20

评论:

2026-3-3,20:24

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by