Home > Net >  save columns of matrix in vector variables in Matlab
save columns of matrix in vector variables in Matlab

Time:10-14

In Matlab (R2021b) I am using some given function, which reads time-dependent values of several variables and returns them in a combined matrix together with a time vector. In the data matrix each column represents one vector of time-dependent values for one variable.

[data,time] = function_reading_data_of_several_values('filename');

For readability of the following code where the variables are further processed, I would like to store these columns in separate vector variables. I am doing it like that:

MomentX = data(1,:);
MomentY = data(2,:);
MomentZ = data(3,:);
ForceX  = data(4,:);
ForceY  = data(5,:);
ForceZ  = data(6,:);

That is working. But is there some simpler (or shorter) way of assigning the column of the matrix to individual vectors? I am asking because in real program I have more than the 6 columns as in the example. Code is getting quite long. I was thinking of something similar to the line below, but that does not work:

[MomentX,MomentY,MomentZ,ForceX,ForceY,ForceZ] = data; %does not work

Do you have any idea? Thanks for help!

Update:


Thanks to the hint here in the group to use tables, a solution could be like this:

...
[data,time] = function_reading_data_of_several_values('filename'); 
% data in matrix. Each column representing a stime dependent variable

varNames = {'MomentX', 'MomentX',...}; % Names of columns 
T=array2table(data','VariableNames',varNames); % Transform to Table 
Stress = T.MomentX/W   T.ForceY/A              
  • Related