- "1x1 matrices"   that's a scalar
- "36 variables"   requires a lot of typing. To save keystrokes I create name with sprintf and use '-regexp', '^p\d{2}'
- "to one file with 36 variables"   do you mean mat-file?
- "each value in one column"   do you mean that the values of the variables are column vectors?
Combining variable from several m files to one
2 views (last 30 days)
Show older comments
Hi,
I have data in 1x1 matrices. There are 178 '.mat' files each file contains 36 variables with same names but different values. I want to create a loop so that I could combine them to one file with 36 variables but 178 values, each value in one column.
Is it possible? If yes how?
Kind Regards, Mustahsan
0 Comments
Answers (1)
per isakson
on 4 Oct 2014
Edited: per isakson
on 4 Oct 2014
"Is it possible?"   yes
"If yes how?"   See example below
Comments:
Example:
nF = 3; % number of files
nV = 4; % number of variables
Create some mat-files
for ii = 1 : nF
for jj = 1 : nV
assign( sprintf( 'p%02d', jj ), ii+jj/100 )
end
save( sprintf( 'mydata%02d', ii ), '-regexp', '^p\d{2}' );
clear( '-regexp', '^p\d{2}' )
end
Read mat-files and concatenate data
p01 = nan( nF, 1 );
p02 = nan( nF, 1 );
p03 = nan( nF, 1 );
p04 = nan( nF, 1 );
for ii = 1 : nF
sas = load( sprintf( 'mydata%02d', ii ), '-regexp', '^p\d{2}' );
p01(ii,1) = sas.p01;
p02(ii,1) = sas.p02;
p03(ii,1) = sas.p03;
p04(ii,1) = sas.p04;
end
Save to one file
save( 'mydata', '-regexp', '^p\d{2}' );
clear( '-regexp', '^p\d{2}' )
where
function assign( Name, Value )
assignin( 'caller', Name, Value );
end
I don't recommend the use of the function assign
See Also
Categories
Find more on Workspace Variables and MAT-Files 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!