이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi to all,
L1=4 L2=3 ... L30=12
for i=1:30 P(:,i)=sprintf(L%1d,i) - sprintf(L%1d,i+1) end
The result should be P(1)=L1-L2, P(2)=L2-L3, P(3)=L3-L4, P(4)=L4-L5 but it's not :) What should I use instead sprintf?
Thank you for you answers!!!
채택된 답변
Kevin Holst
2012년 3월 22일
To do this like you're attempting requires the use of the eval function, but be very careful in its use. Google 'eval matlab' for more information on why. And actually, the way you've got it set up wouldn't work. Your for loop needs to run from 1:29 because there's no L31.
for i = 1:29
eval(['P(' num2str(i) ')=L' num2str(i) '-L' num2str(i+1) ';'])
end
Is there any reason why your L variables aren't in a single L vector?
댓글 수: 7
Thomas
2012년 3월 22일
Yes, Be very careful on using the 'eval' command.. I almost got labelled a 'troll' for suggesting it on the forum :)..
http://www.mathworks.com/support/tech-notes/1100/1103.html
Kevin Holst
2012년 3월 22일
Haha, I've suggested it a couple of times now. I always try to point out that it's dangerous, but it's not like eval = Voldemort (or the dark side, pick your metaphor).
Oleg Komarov
2012년 3월 22일
Eval:
- is not necessary in 99.99% of the cases.
- it is not a feasible solution to scale the dimensionality of your project.
- it is obscure
- it can be dangerous if used maliciously
I dare you execute:
eval(eval(eval('char([102 108 105 112 108 114 40 39 116 105 120 101 39 41])')))
- it can generate errors hard to debug (thus slowing your progress significanlty).
An example: say you have 10 filters and you use
eval([sprintf('filter%d',1:10),'=rand(10)'])
Now, while filter1 is not a matlab function filter2 is.
Suppose you need to use filter2 and try to call it, while having a variable in the editor with the same name...
Although sounds trivial to solve, when your project grows rapidly and you keep generating variables like crazy you don't even see them in your workspace anymore. This nightmare took away a week of debugging (true story).
Kevin Holst
2012년 3월 22일
I couldn't agree more, hence the question about why L1,2,3,4 isn't just a vector. But when it's the only option, you need to be able to use it properly and recognize possible pitfalls, like you filter example.
Oleg Komarov
2012년 3월 22일
@Kevin have you tried eval(eval(eval... @trollface :)
I only use evalin (not eval) when I have to swap variables to the 'base' workspace.
I only used to use eval when I needed to convert '2' to 2 (str2num), but then came str2double.
Kevin Holst
2012년 3월 22일
@Oleg, I haven't tried the eval(eval(eval(... suggestion, but I did evaluate char([102 108 105 112 108 114 40 39 116 105 120 101 39 41]) in my workspace... very sneaky! ;) That's the nice way to show a problem with eval. The mean way would be to "accidentally" have someone recursively remove all of the contents of their hard drive. That'd be a bad day.
Oleg Komarov
2012년 3월 22일
Should have concealed it better...
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
