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

 Accepted Answer

Much simpler:
D = 'C:\Users\erik.from\Documents\MATLAB\Event';
S = dir(fullfile(D,'*.xlsx'));
for k = 1:numel(S);
F = fullfile(D,S(k).name);
S(k).data = xlsread(F);
end
All of the file data are stored in the structure S. For the example for the third file:
S(3).name % the filename
S(3).data % the file data

9 Comments

Okay, however, I get an error message about an undefined variable F since you use it in the for loop before defining it.
F = fullfile(F,S(k).name);
should be
F = fullfile(D,S(k).name);
The same problem holds for both of the answers, even with the edited
filenames = cellfun(@(S) strcat(projectdir, '\', S), {D.name}, 'uniform', 0);
"I get an error message about an undefined variable F since you use it in the for loop before defining it."
Of course the first input to fullfile needs to be the directory path. I corrected my answer.
From this, it seems that there is a problem with the file. However, by looking at the files as they are saved, everything looks normal.
Thanks for all the help! It seems that it was the computer that had gone into complete meltdown and locked this excel-file. A simple restart solved it!
And the one with the funny name really was there.
The file whose name is prefixed '~$' is an MS Office "owner file", which are temporary files created by MS Office applications Word and Excel. It indicates which user has a lock on a file. Normally the "owner file" is deleted by MS Office when the actual data file is closed, but if for some reason the application does not close normally, the "owner file" can hang around and cause problems (particularly on shared network drives).

Sign in to comment.

More Answers (1)

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

Thank you for your response. I see that your example has some differences to the one I showed. Particulary the fullfile which is the directory separator if I have understood correctly. From the permalink I added the contributor talked about a filesep but I didn´t really understand the implementation. Does both fullfile and filesep achieve the same outcome, and/or are there any important limitations to note with them?
Furthermore, I seem to get this error.
Regards
Erik
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.
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.
This gives a similar error message to the one I got in the beginning. It adds a $ since there is a problem with filesep.
Well then try
filenames = cellfun(@(S) strcat(projectdir, '\', S), {D.name}, 'uniform', 0);

Sign in to comment.

Categories

Asked:

on 7 Oct 2020

Commented:

on 7 Oct 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!