Using Dir command together with xlsread troubleshooting
Show older comments
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
Accepted Answer
More Answers (1)
Walter Roberson
on 7 Oct 2020
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 Comments
Walter Roberson
on 7 Oct 2020
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
on 7 Oct 2020
Walter Roberson
on 7 Oct 2020
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
on 7 Oct 2020
Walter Roberson
on 7 Oct 2020
Well then try
filenames = cellfun(@(S) strcat(projectdir, '\', S), {D.name}, 'uniform', 0);
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


