String to Variable once more

1 次查看(过去 30 天)
Hello,
I am having hard time following a generic code for this. So I will put my own code in and see if anyone can help me: ...
Columns={'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N'};
Rows={'2';'31';'61';'91';'121';'151';'181';'211'};
Labels={'0';'01';'02';'04';'08';'16';'24';'36';'50';'625';'75';'875';'1';'1125'};
for i=1:14;
Var1=strcat('Rand',Labels(i,1));
Var2=strcat('Delta',Labels(i,1));
C1=strcat(Columns(i,1),Rows(j,1));
if exist(Var1{1,1},'var')==1 && exist(Var2{1,1},'var')==1
xlswrite(filePattern4,genvarname(Var1{1,1}),'Sheet1',C1{1,1});
xlswrite(filePattern4,genvarname(Var2{1,1}),'Sheet2',C1{1,1});
end
end
The variables Rand0, Rand01, etc. are defined at the beginning of the code but they are case dependent so some of them are not always there. That is why I use exist. I thought genvarname would work to convert string into variable but it didn't. Could anyone help with this? Thank you!

采纳的回答

Walter Roberson
Walter Roberson 2012-4-12
genvarname() was an incorrect solution. Please see Thomas's update, and Jan's answer.
Dynamic structure field names would be a better choice.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by