Build a matrix from saved arrays with differents lengths

2 次查看(过去 30 天)
Hi to all!
I'm working on a simulation program. Each time the programs ends a simulation it saves the data (an array sized of 1 row and multiple columns) to a single .mat file: 'rf_ln1.mat'
For each simulation array size can change.
So, now: How can I construct a matrix from this saved different sized arrays? Because I don't know the size of each saved line unless I load them.
For example: rf_ln1.mat, rf_ln2.mat... rf_ln129.mat M = [rf_ln1.mat rf_ln2.mat ... rf_ln129.mat];
1. Is there an easy way such as using a struc or cell or a function? 2. Any tips? Ideas? Has someone already done this?
I would do: load file save it into a struc or cell (o create a variable with the name) seach the biggest one create a zero matrix sized (1,biggestone) create for each array a new one with the max size of all arrays insert it in the zero matrix
Any ideas with no so much steps?
Thanks for your answers! :)

采纳的回答

Walter Roberson
Walter Roberson 2012-1-23
You can use whos() with the -file option in order to find the matrix sizes without loading the files.
  2 个评论
Daniel
Daniel 2012-1-23
Is there a form to get in a variable the size of rfline_post (in this case)?
>> whos -file rf_data/rf_post_ln1.mat
Name Size Bytes Class Attributes
rfline_post 18173x1 145384 double
tline_post 1x1 8 double
Walter Roberson
Walter Roberson 2012-1-23
http://www.mathworks.com/help/techdoc/ref/whos.html
s = whos('rfline_post', '-file', 'rf_data/rf_post_In1.mat');
s.size

请先登录,再进行评论。

更多回答(1 个)

Daniel
Daniel 2012-1-24
Hi,
Here is how i have done it. Thanks Walter! :)
lineLengths = [];
for i=1:no_lines
fileName = ['rf_data/rfline_pre_' num2str(i) '.mat'];
s_pre = whos('rfline_pre', '-file',fileName );
lineLengths_pre=[lineLengths s_pre.size];
fileName = ['rf_data/rfline_post_' num2str(i) '.mat'];
s_post = whos('rfline_post', '-file',fileName );
lineLengths_post=[lineLengths s_post.size];
end
maxLength_pre = max(lineLengths_pre);
maxLength_post = max(lineLengths_post);
rfAllLines_pre = zeros(maxLength_pre,no_lines);
rfAllLines_post = zeros(maxLength_post,no_lines);
tlineAll_pre = [];
tlineAll_post = [];
for i=1:no_lines
fileName = ['rf_data/rfline_pre_' num2str(i) '.mat'];
cmd = ['load ', fileName];
eval(cmd);
rfAllLines_pre(1:max(size(rfline_pre)),i)=rfline_pre;
tlineAll_pre = [tlineAll_pre tline_pre];
fileName = ['rf_data/rfline_post_' num2str(i) '.mat'];
cmd = ['load ', fileName];
eval(cmd);
rfAllLines_post(1:max(size(rfline_post)),i)=rfline_post;
tlineAll_post = [tlineAll_post tline_post];
end

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by