How do I use an array of files?
25 views (last 30 days)
Show older comments
I have this code below which I am trying to make an array of files to use in a loop so I don't have to rewrite the same code over and over again, but when I use this method the array only pulls the first letter of the file name making it impossible to access that file. The fname array is the one I am trying to specify, but when I make it a string array it does not recognize the names as a file. Thanks in advance.
L = 50.8; %Support Span as measured in mm.
b = 24.75; %Width of rock as measured in mm.
d = 10.2; %Thickness of rock as measured in mm.
fname = ['Specimen_RawData_1.xls','Specimen_RawData_2.xls',...
'Specimen_RawData_3.xls','Specimen_RawData_4.xls',...
'Specimen_RawData_5.xls','Specimen_RawData_6.xls',...
'Specimen_RawData_7.xls','Specimen_RawData_8.xls',...
'Specimen_RawData_9.xls','Specimen_RawData_10.xls',...
'Specimen_RawData_11.xls','Specimen_RawData_12.xls',...
'Specimen_RawData_13.xls','Specimen_RawData_14.xls',...
'Specimen_RawData_15.xls','Specimen_RawData_16.xls',...
'Specimen_RawData_17.xls','Specimen_RawData_18.xls',...
'Specimen_RawData_19.xls','Specimen_RawData_20.xls',...
'Specimen_RawData_21.xls','Specimen_RawData_22.xls',...
'Specimen_RawData_23.xls','Specimen_RawData_24.xls',...
'Specimen_RawData_25.xls'];
for i = 1:25
a = xlsread(fname(i));
x = a(:,3);
y = a(:,4);
[ra,ca,va] = find(y > i);
ta = ra(1);
[rb,cb,vb] = find(y > i - 10);
tb = rb(1);
m = ((y(ta)-y(tb))/(x(ta)-x(tb)))/1e3
E = ((L^3)*m)/(4*b*d^3)
n = i;
plot(n,E,'*')
end
0 Comments
Accepted Answer
Oleg Komarov
on 26 Mar 2012
Implement these changes:
% Fname as a cell array of strings
fname = {'Specimen_RawData_1.xls',...
...}
% note the curly brackets
a = xlsread(fname{i});
More Answers (1)
Matt Tearle
on 26 Mar 2012
Strings in MATLAB are treated as a matrix of characters. Hence,
fname = ['Specimen_RawData_1.xls','Specimen_RawData_2.xls']
will make a 1-by-44 character array containing the string 'Specimen_RawData_1.xlsSpecimen_RawData_2.xls' (because you concatenated the two strings (char arrays) together horizontally).
So first fix is to concatenate them vertically into a matrix of characters:
fname = ['Specimen_RawData_1.xls';'Specimen_RawData_2.xls']
(Note the ; instead of ,). Now you have a matrix of two rows, one per string. To index into each filename in the loop, then, use
a = xlsread(fname(i,:));
Now, if your filenames have different lengths, you will not be able to concatenate vertically (all rows of a matrix must have the same number of columns).
So another approach is to use a cell array of strings:
fname = {'Specimen_RawData_1.xls','Specimen_RawData_2.xls'}
Then use cell indexing in the loop:
a = xlsread(fname{i});
Finally, you may want to investigate the functions ls and dir. You may also want the cellstr function for converting a char array to a cell array of strings.
0 Comments
See Also
Categories
Find more on Cell Arrays 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!