Read multiple xls files in a for loop

Hello! I was trying to get 3 excel files to read through a for loop and do a simple function. The dates (mm/dd/yy) read into matlab code and I'd like to spit them back out as new variables that translate them back into dates via the datestr function. I know its my coding that is the problem and I've tried manipulating it several times with no success so any suggestions would be greatly appreciated! Thanks!
source_dir = '/Users/student/Documents/MATLAB/RAWS Station Data';
dest_dir = '/Users/student/Documents/MATLAB/RAWS Results';
source_files = dir(fullfile(source_dir, '*.xls'));
n=length(source_files);
i=n;
for i=1:n
a= xlsread(source_files);
d = a(:,1);
ds(i) = datestr(d,2);
end

 采纳的回答

Please use the debugger to find out more details:
dbstop if error
Then run the program until it stops and check:
size(datestr(d, 2))
size(ds)
Do the number of elements match?

4 个评论

Hey Jan! I went ahead and ran it and the two sizes do indeed matchup! The debug error came up with:
Undefined function or variable 'data'.
Error in Run (line 29) n=length(data) %total length of avg mean/max/min temps
My full code is still the same:
bstop if error
source_dir = '/Users/student/Documents/MATLAB/RAWS Station Data'; % Source directory
dest_dir = '/Users/student/Documents/MATLAB/RAWS Results'; % Result Destination directory
source_files = dir(fullfile(source_dir, '*.xls')); %Locates all .xls files
n=length(source_files); %total length of RAWS files to be used in loop
i=n; % defines variable 'i' for for loop
for i=1:n
if i <= n
a = xlsread(source_files(i).name);
d = a(:,1);
ds(i) = datestr(d,2); %Converts numeric Matlab code for dates back into original format
end
end
Please format your code properly to improve the readability. Which line causes the error?
Sorry about that! I haven't properly pasted code into here before so I'm hoping this works better:
dbstop if error
source_dir = '/Users/student/Documents/MATLAB/RAWS Station Data'; % Source directory
dest_dir = '/Users/student/Documents/MATLAB/RAWS Results'; % Result Destination directory
source_files = dir(fullfile(source_dir, '*.xls')); %Locates all .xls files
n=length(source_files); %total length of RAWS files to be used in loop
i=n; % defines variable 'i' for for loop
for i=1:n
if i <= n
a = xlsread(source_files(i).name);
d = a(:,1);
ds(i) = datestr(d,2); %Converts numeric Matlab code for dates back into original format
end
end
It's telling me that:
Error in Run (line 29) n=length(data) %total length of avg mean/max/min temps
So line 9 of the pasted code above starting at n=length(source_files). All lines above that in my Matlab script are just comments
Now we see, that there is an error in the "line 29: n=length(data)", but what is the error message? And This line does not occur in the posted code.
It is not useful to declare the variable used as a loop counter before:
%Omit this: i=n; % defines variable 'i' for for loop
for i = 1:n
...
Inside the loop, i goes from 1 to n, so you do not have to check "if i <= n".

请先登录,再进行评论。

更多回答(1 个)

Evan
Evan 2013-7-3
编辑:Evan 2013-7-3
Is this any better?
source_dir = '/Users/student/Documents/MATLAB/RAWS Station Data';
dest_dir = '/Users/student/Documents/MATLAB/RAWS Results';
source_files = dir(fullfile(source_dir, '*.xls'));
n=length(source_files);
for i=1:n
a = xlsread(source_files(i).name);
d = a(:,1);
ds(i) = datestr(d,2);
end
Your problem looks to be caused by the fact that source_files is an nx1 struct, where n is the number of .xlsx files in your directory. To read in each .xlsx file one at a time, you have to loop through that struct, accessing its "name" field in your call to xlsread.

5 个评论

Thanks Evan! I didn't know that was how to shuffle through .xls file names in Matlab. I'm still not sure what to do about the error it gives me after correcting the code though:
Subscripted assignment dimension mismatch.
Everything seems to work smoothly up until
ds(i) = datestr(d,2);
Yep, any time you have a structure and you want to loop through it, you use syntax like a did above.
Also, try this for ds:
ds(i,:) = datestr(d,2);
Hey Evan! I tried modifying the code as suggested but it's still producing the same error unfortunately :(
Hmmm. Interesting. Just out of curiosity, is this a script or a function? If it's just a script, are you calling clear before running again?
Its in a script since I'll need to add more to my for loop eventually. I've called clear and clearvars and it still produces the same result. I'm just trying to get it to create a ds(1) vector, ds(2) vector, ds(3), etc.
Subscripted assignment dimension mismatch.
Error in Run (line 36)
ds(i,:) = datestr(d,2);

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by