Is there an efficent way to assign names to the vectors in a matrix
Show older comments
I would like to assign a vector of names to the vectors of a matrix. I have the data in a matrix, but I need to be able to access it as vectors using predetermined names. I have the following vector of variables:
[TIM PAN STAT VB VR IBI] (shorted for convenience)
and a Nx6 data matrix. I want to assign each of the column vectors in this data matrix to the variable in the corresponding row of the variable matrix.
bei. TIM=data(:,1), PAN=data(:,2) etc
these don't work
[TIM PAN STAT VB VR IBI]=data;
or ...
for i=1:size(names,2)
names(i)=result(:,i);
end
Any help is appreciated. I always seem to run into this kind of problem when I try and work with other peoples code :(
Accepted Answer
More Answers (1)
Jan
on 1 Nov 2011
If the list of names can be changed dynamically:
You can use a struct with dynamic fieldnames instead:
Names = {'TIM', 'PAN', 'STAT', 'VB', 'VR', 'IBI'};
for i = 1:numel(names)
Data.(names{i}) = result(:, i);
end
disp(Data.TIM);
If the list of names is fixed, see Walter's answer.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!