Alternate form of eval function

조회 수: 4 (최근 30일)
Wiqas Ahmad
Wiqas Ahmad 2021년 3월 18일
댓글: Wiqas Ahmad 2021년 3월 18일
How can I write these two syntaxes in alternate from?
eval(['I',num2str(i),num2str(j),num2str(k),'=','getsiganl(filename1)',';']);
eval(['II',num2str(i),num2str(j),num2str(k),'=','smooth(sum(I',num2str(i),num2str(j),num2str(k),',2)',');']);
  댓글 수: 1
Stephen23
Stephen23 2021년 3월 18일
편집: Stephen23 2021년 3월 18일
Rather than trying to use an "Alternate form of eval function", you should just write your code using much simpler and much more efficient indexing. Is there a particular reason why you cannot use indexing?

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

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2021년 3월 18일
You'll have to learn good programming-habits and utilise the vectorized capabilities of matlab. This looks like a case where you might get away with multi-dimensional arrays - but that would require all your signals to have the same length. The safer option is to store all signals in a cell-array - there everything goes. Perhaps something like this:
I_all{i,j,k} = getsiganl(filename1);
I_all_smooth{i,j,k} = smooth(sum(I_all{i,j,k},2));
I would also suggest changing the indices i, j and k to i1, i2 and i3 - this makes me more comfortable when it comes to which order they should go in indexing and also leaves i and j for the imaginary constant.
HTH

추가 답변 (1개)

Matt J
Matt J 2021년 3월 18일
Why not simply
I(i,j,k)=getsiganl(filename1);
II(i,j,k)=smooth(sum(I(i,j,k),2))
Be mindful though that if i,j,k are scalar coordinates, neither the original eval form nor the above make sense.
  댓글 수: 2
Bjorn Gustavsson
Bjorn Gustavsson 2021년 3월 18일
If getsiganl returns a 2-D matrix it kind of makes sense, note that the eval-version would generate variables with names like I123 for i = 1, j = 2 and k = 3 and then the completely inuntangleable _I1234 for all of [i, j, k] = [12, 3 4], [1 23 4] or [1 2 34]...
Wiqas Ahmad
Wiqas Ahmad 2021년 3월 18일
@Matt J I think I have checked this in prior but haven't worked. Anyway I got the right answer. Thank you

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

카테고리

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