Combining variable from several m files to one
Mostrar comentarios más antiguos
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
Respuestas (1)
per isakson
el 4 de Oct. de 2014
Editada: per isakson
el 4 de Oct. de 2014
"Is it possible?"   yes
"If yes how?"   See example below
Comments:
- "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?
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
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!