Is it possible to copy the elements of a matrix to a set of variable names ?
Show older comments
Given is a matrix A of size [1x6]. What I want is to copy the elements of A to a set of variable names [a,b,c,d,e,f], so that
a = A(1);
b= A(2)
a.s.o.
I tried
[a,b,c,d,e,f] = deal(A);
But this copies not the elements but the hole matrix A into each variable.
Any help is much appreciated.
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 13 Mar 2013
Edited: Azzi Abdelmalek
on 13 Mar 2013
A=rand(1,6)
s='abcdef'
for k=1:numel(s)
assignin('base',s(k),A(k))
end
6 Comments
Alexander
on 13 Mar 2013
Azzi Abdelmalek
on 13 Mar 2013
A=rand(1,6)
s='abcdef'
arrayfun(@(x) assignin('base',s(x),A(x)),1:numel(s))
Azzi Abdelmalek
on 13 Mar 2013
Edited: Azzi Abdelmalek
on 13 Mar 2013
%or
arrayfun(@(x,y) assignin('base',x,y),'a':'f',A)
Alexander
on 13 Mar 2013
Azzi Abdelmalek
on 13 Mar 2013
Post your function
Alexander
on 13 Mar 2013
Categories
Find more on Structures 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!