How to generate names?

I want to make my code shorter.
I have this code:
Sheetname1=txt1{1,3};
Sheetname2=txt2{1,3};
Sheetname3=txt3{1,3};
Sheetname4=txt4{1,3};
Sheetname5=txt5{1,3};
Sheetname6=txt6{1,3};
instead there should be a for loop
for i=1:6
Sheetname'write i'=txt'write i'{1,3};
end
how to :) ?

댓글 수: 1

Hassan F
Hassan F 2013년 1월 8일
Is there a specific reason why you implemented your script this way? Such a for loop will be slow and hard to read later on.

댓글을 달려면 로그인하십시오.

답변 (5개)

Hassan F
Hassan F 2013년 1월 8일

0 개 추천

You can use eval
for i=1:6
eval(['Sheetname' num2str(i) '=txt' num2str(i) '{1,3};'])
end

댓글 수: 5

Walter Roberson
Walter Roberson 2013년 1월 8일
... but it is not advised.
Hassan F
Hassan F 2013년 1월 8일
Well yes, this is slow and hard to read. But for creating just a handful of variables it shouldn't matter.
Matt J
Matt J 2013년 1월 8일
편집: Matt J 2013년 1월 8일
No, it's still not advised because
  • You're introducing variables non-explicitly into the workspace (so-called poofing). This creates hazards discussed here.
  • Enumerating variables through their names makes them hard to index later
The better thing to do here would be
for i=1:6
Sheetname{i} = eval(['txt' num2str(i) '{1,3};'])
end
Jan
Jan 2013년 1월 8일
편집: Jan 2013년 1월 8일
I do think that it matters. Programmers get used to use methods they are used to use. The more EVAL you write, the more acceptable does this command look, because we are human.
Much better in theory, practice and from the viewpoint of training the brain to think in efficient ways:
Sheetname{1} = txt{1,3};
Sheetname{2} = txt{2,3};
Sheetname{3} = txt{3,3};
Sheetname{4} = txt{4,3};
Sheetname{5} = txt{5,3};
Sheetname{6} = txt{6,3};
Never include an index in the name of a variable. You would not include your current weight to your name also, because the inconveniences are obviously.
Kind regards, Jan70.6kg
Hassan F
Hassan F 2013년 1월 8일
Totally makes sense.
Hassan74.0kg

댓글을 달려면 로그인하십시오.

Hello kity
Hello kity 2013년 1월 8일

0 개 추천

I tried this : >>
for i=1:6
A=['Sheetname' sprintf('%d' , i)];
B=['txt' sprintf('%d', i)];
A(i)=B{1,3}(i);
end
??? Cell contents reference from a non-cell array object.
somehow it gives error about B{1,3}
while
>> txt1{1,3}
ans =
T1

댓글 수: 1

Hassan F
Hassan F 2013년 1월 8일
편집: Hassan F 2013년 1월 8일
See my answer http://www.mathworks.se/matlabcentral/answers/58253#answer_70475, but you should avoid this solution and use cells or other classes which suits your data the most.

댓글을 달려면 로그인하십시오.

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 8일

0 개 추천

A=cell(1,6);
for ii=1:6
A{ii}=sprintf('Sheetname%d' , ii)
end
Image Analyst
Image Analyst 2013년 1월 8일

0 개 추천

My m-files are usually 2-3 thousand lines long. I never worry about saving a paltry 3 lines. Why do you care? If you want to use fewer variables, say use a cell array for Sheetname instead of 6 variables, then you could do that, but with only 6 variable, it's really no big deal. Now if you had dozens or hundred variables named like that (which we see sometimes).....then yes, you'd want an array, but I'm not going to ding you for only 6 uniquely named variables.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2013년 1월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by