Using Dir command together with xlsread troubleshooting

2 views (last 30 days)
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

Stephen23
Stephen23 on 7 Oct 2020
Edited: Stephen23 on 7 Oct 2020
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
Stephen23
Stephen23 on 7 Oct 2020
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)

Walter Roberson
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
Erik From
Erik From on 7 Oct 2020
This gives a similar error message to the one I got in the beginning. It adds a $ since there is a problem with filesep.
Walter Roberson
Walter Roberson on 7 Oct 2020
Well then try
filenames = cellfun(@(S) strcat(projectdir, '\', S), {D.name}, 'uniform', 0);

Sign in to comment.

Categories

Find more on Environment and Settings 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!