How to use a string as name for variable/array?

조회 수: 44 (최근 30일)
Daniel Barrera
Daniel Barrera 2018년 10월 3일
편집: Stephen23 2018년 10월 5일
Hi, I'm trying to assign titles to a bunch of arrays extracted from columns in an excel sheet. I used: Data_raw=readtable('nameoffile.xslx'); Colum_names=Data_raw.Properties.VariableNames; To extract the titles of each column I tried to use loop to go throuhg all the arrays [m,n]=size(Data_raw); for i=1:n Char(Column_names(1,i))=table2array(Data_raw(:,i)); end
I'm not sure if there is a way to make it work I tried to use the genvarname function or using {} but no luck. I know its not a complicated question but any guidance will be appreciated. Thank you.
  댓글 수: 3
Daniel Barrera
Daniel Barrera 2018년 10월 3일
Thank you for saving me an ordeal :), that article was also very insightful.
Stephen23
Stephen23 2018년 10월 3일
@Daniel Barrera: I hope that it was interesting and useful: the more you read, the more you will learn about how to write efficient MATLAB code and making your own life easier:
Please also feel free to vote for my tutorial and any of the answers that I wrote in it, if they were useful for you. Support is always welcome :)

댓글을 달려면 로그인하십시오.

답변 (1개)

Raghunandan V
Raghunandan V 2018년 10월 5일
I think the answer for this question is already provided here
https://in.mathworks.com/matlabcentral/answers/35295-how-to-convert-a-string-into-a-variable
Please check
  댓글 수: 1
Stephen23
Stephen23 2018년 10월 5일
편집: Stephen23 2018년 10월 5일
... which is how some beginners force themselves into writing slow, complex, buggy code:
It is easier to write simpler, neater, more efficient code using indexing, fieldnames, etc..

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by