How do I output a vectors in the order for example, z1, z2,z3,z4,z5....after having a 1 x 5 cell array go through a for loop?

2 次查看(过去 30 天)
Hello,
Lets say I have a cell array Z, and it contains the following. This is just a simple example.
Z = {1,4,13,2,6}
How can I write a for loop so that it will output five vectors in numerical order, like Z1,Z2,Z3,Z4,Z5 such that each vector contains the values in each cell in Z. The following output would be ideal.
Z1 = 1
Z2 = 4
Z3 = 13
Z4 = 2
Z5 = 6
I am using 2015a btw. Any help would be appreciated. Thanks!

采纳的回答

Star Strider
Star Strider 2015-8-13
Recent versions support cells automatically doing the same as the deal function:
Z = {1,4,13,2,6};
[Z1,Z2,Z3,Z4,Z5] = Z{:}
  5 个评论
George Vuong
George Vuong 2015-8-13
编辑:George Vuong 2015-8-13
Hey Walter Roberson, that link you provided actually helped answer my question. And so did Star Strider's first response. It contains valuable information and it is a reference I would suggest any beginner learning how to use MATLAB to look at.
Anyhow, despite all the feedback and suggestions telling me to not go the route my original question had requested, and looking at the link you provided, here is what I came up with and it worked! Lol.
Z = {1,2,3,4,5,6,7,8,9,10};
for i = 1:length(Z)
eval(sprintf('Z%d = Z{i}', i));
end
In all seriousness thanks for your help Star Strider and Walter Roberson. I'll definitely reconsider if I ever want to output variables A1, A2, A3,....A50.

请先登录,再进行评论。

更多回答(1 个)

Stephen23
Stephen23 2015-8-13
编辑:Stephen23 2019-6-19
  2 个评论
George Vuong
George Vuong 2015-8-13
Hey Stephen Cobedick,
After reading and reviewing your response and material provided, which I gladly appreciate, I am more or less, convinced that creating dynamic output variables is a big, "no-no". And after learning how to program with MATLAB day after day, it makes more and more sense why it is not suggested. Rest assured, if anybody ever comes across a question similar to the one I posted, then the link to this thread would be my valid response.
I will also definitely take a look at vectorized coding. I may already be using it in some cases, but the term itself is new to me.
And yes, ugh, I can imagine how difficult and complicated it would be to try to pass hundreds of dynamically named variables to a function..
Creating a table is definitely a good alternative in terms of organizing and managing large sets of data.
Even though it was not necessary for you to do so, it was really nice of you to provide additional links regarding how other programming languages also suggest not creating dynamically named variables. To me, I find that to be very useful if I decide to learn coding in another language. It's great how what I am learning in MATLAB, mainly from you all, can translate to other programming languages as well.
So thank you.
Stephen23
Stephen23 2015-8-14
编辑:Stephen23 2015-8-14
My pleasure. I hope that those links were interesting to read. Basically when you consider doing this:
X1 = ...
X2 = ...
X3 = ...
you can see that the 1, 2, 3, etc, are really just indices... so then why not avoid all of the slowness, bugginess and obfuscation of using eval and just make them actual indices:
X(1) = ...
X(2) = ...
X(3) = ...
Much faster, easier to debug, easier to search for in your files, and MATLAB Editor provides pop-ups with the values, instance highlighting, etc, etc.
You will not regret learning how to program without eval!

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by