convert a variable string to data

6 次查看(过去 30 天)
mohammad
mohammad 2011-9-11
it's needed to convert a variable string to data. for example NAME is string and in a loop could be i=1>>NAME='abd', i=2>>>NAME='fdr',.... now how to convert this string to data. its needed this variable string being matrix.
for example:
q=[];
for i:5
q=[q;i+1];
NAME=q; % i know this is wrong because NAME is string but here i need converting string to data
end
  2 个评论
Fangjun Jiang
Fangjun Jiang 2011-9-11
What do you mean "string to data"? If NAME='abd', what should the data be?
mohammad
mohammad 2011-9-11
I mean a thing like matrix, like d=[1 3 4 5], I need NAME=d but i know this is wrong but I need this. there is a command with name of eval but in this way i must first convert d to string like eval(sprntf('%s=d',NAME). is there another way?

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2011-9-11
Please do not do that. Please read this FAQ instead.
  2 个评论
mohammad
mohammad 2011-9-11
thanks, but i must do, i don't want make names of a matrix or other thing, numbering. NAME varies with varying of 'i' in loop but the name of that not depends on 'i'.for example NAME is string and in a loop could be i=1>>NAME='abd'(NOT NAME1), i=2>>>NAME='fdr'(NOT NAME2),...
because its the name of a .xls file that in each loop another .xls file be read.
mohammad
mohammad 2011-9-11
Walter, is there other way except eval(sprintf('%s=MATRIX',NAME))
MATRIX is a defined matrix

请先登录,再进行评论。


Fangjun Jiang
Fangjun Jiang 2011-9-11
If you really need to do this, eval(),evalin() or assignin() is the function to use. This is the same question as your previous one. There is no other way. But I highly recommend you read the link I provided. You could use structure which is very close to what you want.
Name={'abd','fdr'};
data=struct;
for k=1:length(Name)
data=setfield(data,Name{k},rand);
end
data =
abd: 0.65375734866856
fdr: 0.49417393663927
  2 个评论
mohammad
mohammad 2011-9-11
thanks, sprintf:Format data into string. and i want opposite of sprint. maybe a command like dprintf :)
but there is not this command in matlab!
Fangjun Jiang
Fangjun Jiang 2011-9-11
Well, for numeric data/digits, there is.
a=num2str(3.14159)
b=str2num(a)

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by