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

Stephen23
Stephen23 2019년 5월 17일
편집: Stephen23 2019년 5월 17일
This is not valid MATLAB syntax:
B=(sample1,sample2, sample3)
What are you actually trying to do?
Note that using numbered variables is a sign that you are doing something wrong.
joms
joms 2019년 5월 17일
편집: joms 2019년 5월 17일
sample1=rand(3);
sample2=rand(3);
sample3=rand(3);
This is correc syntax (B is an array)
[B]=[sample1, sample2, sample3]
madhan ravi
madhan ravi 2019년 5월 17일
편집: madhan ravi 2019년 5월 17일
That is obviously Python syntax for lists
edit: Looks like you have changed () to [] after I made this comment.
joms
joms 2019년 5월 17일
i edited it please check. Thanks
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일

0 개 추천

A={'sample1' 'sample2' 'sample3'} ;
a = array2table(rand(3)); % an example
a.Properties.VariableNames=A

댓글 수: 7

Or simply:
B=array2table(rand(3),...
'VariableNames',A)
joms
joms 2019년 5월 17일
편집: joms 2019년 5월 17일
thanks for the answer but i dont need "a" value in my code. I just want a dynamically changing variable name based on its string order inside a parenthesis() for my code to work
B=(sample1,sample2, sample3)
madhan ravi
madhan ravi 2019년 5월 17일
Now that's a bad approach why do you need that to do ? as Stephen mentioned in the comment above.
joms
joms 2019년 5월 17일
편집: joms 2019년 5월 17일
I have a list of variable from seperate excel file. The order of the list is changed sometime via user. From the order the m file will find matching label name in seperate excel file and output the column under it.
I have the rest of the code but the conversion of cell array into [sample1,sample2, sample3] i have none
madhan ravi
madhan ravi 2019년 5월 17일
편집: madhan ravi 2019년 5월 17일
Use readtable() to read the file and match the columns using ismember() in the column subscript of the table.
https://in.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html#bt1552n-1 - see those example to access datas from table also see under Extract Multiple Rows and Multiple Variables for your purpose.
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
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개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

릴리스

R2013b

질문:

2019년 5월 17일

댓글:

2019년 5월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by