Error in using eval statement

조회 수: 5 (최근 30일)
Ramesh Bala
Ramesh Bala 2022년 1월 10일
편집: Stephen23 2022년 1월 10일
Why it shows an incomplete statement ?
I'm trying to write this statement using eval as follows : Energy_H_A1S1 = rms (H_A1S1); but in eval it shows error ?
Thanks in advance.
eval(['Energy_H_A',num2str(1),'S',num2str(1),' = rms(H_A',num2str(1),'S',num2str(1);]);
  댓글 수: 2
Stephen23
Stephen23 2022년 1월 10일
편집: Stephen23 2022년 1월 10일
"I'm trying to write this statement using eval as follows : Energy_H_A1S1 = ..."
And that is the start of your problems.
Forcing meta-data into variable names makes acessing your data complex, slow, inefficient, and buggy... as your question demonstrates. Meta-data is data. Data should be stored in variables, not in variable names.
Your poor data design forces you into writing bad code, which could be easily avoided by storing the meta-data correctly.
Ramesh Bala
Ramesh Bala 2022년 1월 10일
Thank you Stephen for your valuable reply.Yes that's right using names was wrong as it was indeed slow.

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

채택된 답변

Paul
Paul 2022년 1월 10일
There's almost certainly a better way to organize the data and store the results, but if you must use eval:
['Energy_H_A',num2str(1),'S',num2str(1),' = rms(H_A',num2str(1),'S',num2str(1);]
ans = 'Energy_H_A1S1 = rms(H_A1S1'
should be:
['Energy_H_A',num2str(1),'S',num2str(1),' = rms(H_A',num2str(1),'S',num2str(1),');']
ans = 'Energy_H_A1S1 = rms(H_A1S1);'
  댓글 수: 4
Paul
Paul 2022년 1월 10일
Alternatives are dicussed in the link posted by @Walter Roberson in his comment.
Stephen23
Stephen23 2022년 1월 10일
편집: Stephen23 2022년 1월 10일
"Could you please also post an example of the mentioned better way instead of eval as it takes high running time for this varying A1..4..S1..4 data"
The MATLAB introductory tutorials also show how to use MATLAB effectively by using arrays and indexing:

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 1월 10일
Asking how to use eval without problems is like asking how to use a chainsaw without its chain guard. It would be irresponsible for us to answer in any way other than "Don't do that!"
  댓글 수: 1
Ramesh Bala
Ramesh Bala 2022년 1월 10일
Thank you Walter ,Indeed Eval was causing slowness when runnign the code.Thank you for the link

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

카테고리

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