loading an m.file with a series of matrices
Show older comments
Hi there, I want to load the file (WA1.m) which already contains a couple of matrices I copied from an excel-Database. The matrices are as follows: Section_1, Section_2, Section_3 and so on. Every matrix contains 2 columns. The matrices have different number of rows.
Loading the file can be simply done as follows: load WA1.m
My problem is that I can not call these matrices within a for-loop.
I also want to use the matrix number as a counter: like i=1 for Section_1 and so on.
I read about evalin and eval but could not figure that out by myself.
Thanks in advance.
Answers (3)
Andrei Bobrov
on 1 Jun 2012
better using cell arrays ( C ) for submission matrices
d = who;
d2 = strcat(d(strncmp(d,'Section',7)),',');
C = eval(['{',[d2{:}],'}']);
OR
eg
n = 3;
K = arrayfun(@(ii)randi(50,randi(10),2),1:n,'un',0);
eval( ['[' sprintf('Section_%-d,',1:n) ,'] = K{:}'] );
C = eval(['{',sprintf('Section_%-d,',1:3),'}'])
Dima
on 5 Jun 2012
0 votes
1 Comment
Walter Roberson
on 5 Jun 2012
Please do not use that approach. Instead of using
load WA1.m
use
Sections = load('WA1.m');
Then your matrices become fields of the structure Sections, and you can use dynamic field names, such as
fn = sprintf('Section_%d', 5);
Sections.(fn)
Categories
Find more on Structures 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!