- "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 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
per isakson
2014-10-4
编辑:per isakson
2014-10-4
"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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!