Saving/Working with variables using loop index?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I have some problems with the following piece of code in indexing the names using the loop index. Could you help me?
data1=[1,2,3];
data2=[4,5,6];
data3=[7,8,9]; %they are in the workspace
for h=1:3
% A`h'=data`h'+6
% save A`h' using a name depending on h
end
I can't use cells
0 个评论
回答(1 个)
Geoff Hayes
2015-1-15
Cris - from your code it appears that you are trying to create a variable called A1 as
A1 = data1 + 6;
and do the same for A2 and A3. Is this the case? While you can do this using such functions as sprintf and eval, this isn't recommended because of the challenges in debugging the code when errors appear (among other reasons).
Instead, just create one matrix A which is built from the the three vectors, and then add 6 to it. Something like
A = [data1 ; data2 ; data3];
A = A +6;
Try the above and see what happens!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!