Hi. I want to define repetitive vectors from notepad data using loop

1 次查看(过去 30 天)
I am using an analysis program and it gives me a data in form of steel#.out. I made a code to load this data like
steellayer=#;
for ii = 1:steellayer
load(['Steel' num2str(ii) '.out'])
end
and from this code I have Steel1, Stee2, Steel3, .... Steel#.
Each data is a 1000x3 matrix and I just need second and third column from each matrix.
Second column is 'stress' and third column is 'strain' I want to have 'strain1', 'strain2','strain3'.....,'strain#' and 'stress1','stress2','stress3'...,'stress#' for each matrix
I can define each vector but it is not efficient since # varies by chases and I need to change it all the time.
Therefore, I want to make those vectors automatically no matter what # is.
I tried to make it using 'eval' function like this
for n=1:steellayer
eval(['Strain', int2str(steellayer),' = Steel',int2str(steellayer)'])
end
but I cannot pick a certain column and it just makes same matrix as Steel1.... Steel#.
I would really appreciate for any help.
Thanks.
  2 个评论
Jos (10584)
Jos (10584) 2016-2-24
Don't! Instead of using variables A1, A2, ... Ax, use a single variable, like a cell array A{k}, an array of structs A(k).data, or perhaps a regular arrays A(k,:), to store and process your data. This avoids the evil eval construction.
To generate column headers in a text file simply use fprintf as in
fprintf(fid,'Steel%02d',k)

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2016-2-24
Please read http://www.mathworks.com/matlabcentral/answers/196952#answer_174587 . Stephen has discussed the topic exhaustively.

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by