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 个)
"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.
Djamil
about 4 hours 前
Stephen23
about 2 hours 前
"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
about 2 hours 前
> 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.
类别
在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!