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

 채택된 답변

Stephen23
Stephen23 2020년 10월 7일
편집: Stephen23 2020년 10월 7일
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

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);
What shows up if you
ls *.xlsx
"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).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
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

Erik From
Erik From 2020년 10월 7일
편집: Erik From 2020년 10월 7일
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 using 2010a.
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);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

질문:

2020년 10월 7일

댓글:

2020년 10월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by