String elements of a cell as a name to the array elements of another cell

1 次查看(过去 30 天)
Dear friends, can you plz. give me the solution of the problem which I stated below. I have two cell q{22,1) and each element of this cell is a string array. I have another cell r{22,1} and each element of this cell is an array. Now I want to use the element of 'q' as a name to the corresponding element of 'r' and it will be appear in matlab workspace separately. For an example, q{1,1} element will be the name of the array r{1,1} and appear in matlab workspace. Sincerely, Debjyoti

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-8-16
What is the aim of doing this? this is not recommended, read this http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
  4 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2016-8-17
Now, if you want to know how to do it:
q={1:2 3:4}
r={'a' 'b'}
for k=1:numel(q)
assignin('base',r{k},q{k})
end
But you have to know it's a very bad idea!

请先登录,再进行评论。


John BG
John BG 2016-8-17
Debjyoti
Use the command evalin.
evalin executes whatever you input, as long it's valid syntax.
For instance, let's say you have the following stored
q{1,1}='var1';
do the following
1.
read the contents of the cell that has the names you want to use as names of variables, for instance:
var_name=q{1,1}
2.
let's say that you want to create a variable with name in q{1,1} and the following value:
value=35.6
3.
Now build the string to input evalin
string1=num2str(value);string1=[var_name '=' num2str(value)]
4.
execute evalin
evalin('base',string1)
Now in the workspace you have a new variable, named as the contents of q{1,1} with the chosen value.
Debjyoti
If you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  2 个评论
John BG
John BG 2016-8-17
Debjyoti
Thanks for the accepted answer.
Look for correct answers, some people in this forum try to hit many answers, attempting to maximise the chances of scoring credit, and then we come across telegraphic answers, which are essentially like if
when asking for Uranium atomic number,
the reader gets
Check in the periodic table
and sometimes they even moan and try to get the credit away from the one who took the time and effort to supply a useful answer.
Do not hesitate asking anything MATLAB SIMULINK related you consider worth asking, moreover if it's homework.
This forum is to learn MATLAB, regardless of it's homework or not.
Regards
John BG

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by