Convert Cell to variable name array

48 次查看(过去 30 天)
How can i use cell text as variable name input?
I want to use string variables "A" as variable name to output"B".
A={'sample1' 'sample 2' 'sample3'}
like this:
[B]=[sample1, sample2, sample3]
please suggest code made for 2013 matlab version.
Thanks for your help!
  6 个评论
madhan ravi
madhan ravi 2019-5-17
编辑:madhan ravi 2019-5-17
The creation of those matrices are not a good idea , if the sizes are consitent use a ND array else use a cell array as is, what your doing is risky and vulnerable. Don't believe see the valuable contributions
joms
joms 2019-5-17
my code is just simple label order changer not be used for future troubleshooting. there are 100 plus variables so i need it even it work inefficiently

请先登录,再进行评论。

采纳的回答

madhan ravi
madhan ravi 2019-5-17
A={'sample1' 'sample2' 'sample3'} ;
a = array2table(rand(3)); % an example
a.Properties.VariableNames=A
  7 个评论
joms
joms 2019-5-17
编辑:joms 2019-5-19
i managed to write a working code out of this
clc
mabiki=0.8
sample1=[1 2 3 6 7]';
sample2=[nan nan]';
sample3=[4 nan nan 4]';
sample4=[nan nan nan]';
patie2 = padcat(sample1,sample2,sample3,sample4)
[l,w]=size(patie2)
length1=(mabiki*l)-mabiki;
time= (0:mabiki:length1)'
sample1=patie2(:,1)
sample2=patie2(:,2)
sample3=patie2(:,3)
sample4=patie2(:,4)
patie = table(time,sample3,sample4,sample2,sample3);
Fromexcel={'time' 'sample3' 'sample4' 'sample2'};
dataA = patie{1:end,Fromexcel};
matrixfin=array2table(dataA,'VariableNames',Fromexcel)
List=matrixfin.Properties.VariableNames(all(isnan(matrixfin{:,:})))';
if length(List)>=1;
fprintf(' These labels are not found \n');
List=matrixfin.Properties.VariableNames(all(isnan(matrixfin{:,:})))'
end
Stephen23
Stephen23 2019-5-19
Rather than this complex and inflexible code:
sample1=patie2(:,1)
sample2=patie2(:,2)
sample3=patie2(:,3)
sample4=patie2(:,4)
patie = table(time,sample3,sample4,sample2,sample3);
just use array2table. In fact it seems like most of your code could be replaced by array2table and a few table operations.

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by