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!



