필터 지우기
필터 지우기

Saving/Working with variables using loop index?

조회 수: 1 (최근 30일)
MRC
MRC 2015년 1월 15일
답변: Geoff Hayes 2015년 1월 15일
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

답변 (1개)

Geoff Hayes
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!

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by