loading an m.file with a series of matrices

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)

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
Dima on 5 Jun 2012
Hey there, thank you very much. The problem could be solved likt this, I asked some kolleges about it:
x=evalin('base',['Section_' num2str(jj)]); nameSection=['Section_' num2str(jj)]; assignin ('base', nameSection, x);
Have a nice day Dima

1 Comment

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)

Sign in to comment.

Categories

Asked:

on 1 Jun 2012

Community Treasure Hunt

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

Start Hunting!