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 2026년 3월 3일 12:29
편집: Stephen23 2026년 3월 3일 12:38

3 개 추천

"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

Jan
Jan 2026년 3월 3일 15:01
편집: Jan 2026년 3월 3일 15:39
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.
Djamil
Djamil 2026년 3월 3일 18:03
@Stephen23. Thank You. Can you kindly clarify what you mean by 'other bugs' ?
Stephen23
Stephen23 2026년 3월 3일 20:01
"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.
Walter Roberson
Walter Roberson 대략 17시간 전
> 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 2026년 3월 3일 20:21

0 개 추천

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