Using Dir command together with xlsread troubleshooting
이전 댓글 표시
Hi,I have used the same body as this link. The picture is how I have implemented it. Fairly straight forward. All xlsx files, however some in upper case lettering, and the same structure of naming the files. However, the error message shows that when Matlab extracts the names of the files, it adds $ which then gets an error at xlsread since the og file doesent have a $ in it.
Have I done done something wrong? Forgot to add something? Or maybe something else?
Regards
Erik
채택된 답변
추가 답변 (1개)
Walter Roberson
2020년 10월 7일
You are not constructing the filenames properly.
projectdir = 'C:\Users\erik.from\Documents\MATLAB\Event';
D = dir( fullfile(projectdir, '*.xlsx') );
filenames = fullfile(projectdir, {D.name});
nfiles = length(filenames);
data = cell(nfiles, 1);
for ii = 1 : nfiles
fullname = filenames{ii};
data{ii} = xlsread(fullname);
end
In particular, you used [] to put together the directory name and the entry filenames content, but you did not put a directory separator between the two.
댓글 수: 6
Walter Roberson
2020년 10월 7일
Please check
class(projectdir)
class(D(1).name)
which -all fullfile
which release are you using? fullfile line 39 is comments in current releases, and does not have code similar to what you show in the error message. I suspect that you have a third-party fullfile() that is interfering.
Erik From
2020년 10월 7일
Walter Roberson
2020년 10월 7일
I am not sure that I have that old of a MATLAB installed anywhere...
Try replacing
filenames = fullfile(projectdir, {D.name});
with
filenames = cellfun(@(S) fullfile(projectdir, S), {D.name}, 'uniform', 0);
fullfile() is a bit more convenient than using [] with filesep between the parts. It also knows to strip off extra separators. And in later versions it handles cell arrays.
Erik From
2020년 10월 7일
Walter Roberson
2020년 10월 7일
Well then try
filenames = cellfun(@(S) strcat(projectdir, '\', S), {D.name}, 'uniform', 0);
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



