Combining variable from several m files to one

2 visualizaciones (últimos 30 días)
Mustahsan Majeed
Mustahsan Majeed el 3 de Oct. de 2014
Comentada: Mustahsan Majeed el 6 de Oct. de 2014
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
per isakson el 4 de Oct. de 2014
Editada: per isakson el 4 de Oct. de 2014
"Is it possible?" &nbsp yes
"If yes how?" &nbsp See example below
Comments:
  • "1x1 matrices" &nbsp that's a scalar
  • "36 variables" &nbsp 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" &nbsp do you mean mat-file?
  • "each value in one column" &nbsp 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
  2 comentarios
Mustahsan Majeed
Mustahsan Majeed el 6 de Oct. de 2014
"1x1 matrices" that's a scalar yes it was a vector and I took average mean value of all and made it a scalar
"36 variables" requires a lot of typing. To save keystrokes I create name with sprintf and use '-regexp', '^p\d{2}' I am unable to use the srint command
"to one file with 36 variables" do you mean mat-file? yes I want to make one .mat file with 36 variables and in each variable I want 1 column with each cell representing one value from each of the 178 files (so every variable will be (1x178) matrice
"each value in one column" do you mean that the values of the variables are column vectors? (1x178) is the end format for each varibale
  • thank you very much for your kind help *
Mustahsan Majeed
Mustahsan Majeed el 6 de Oct. de 2014
BTW I have a list of the variables exactly typed in, so I can use them too. But its better to learn how to do it without typing all of them.

Iniciar sesión para comentar.

Categorías

Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by