# Alternate form of eval function

2 views (last 30 days)
Wiqas Ahmad on 18 Mar 2021
Commented: Wiqas Ahmad on 18 Mar 2021
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 CommentShowHide None
Stephen23 on 18 Mar 2021
Edited: Stephen23 on 18 Mar 2021
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 on 18 Mar 2021
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
##### 2 CommentsShowHide 1 older comment
Wiqas Ahmad on 18 Mar 2021
Edited: Wiqas Ahmad on 18 Mar 2021
@Bjorn Gustavsson Thank you. This is really what I wanted

### More Answers (1)

Matt J on 18 Mar 2021
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 CommentsShowHide 1 older comment
Wiqas Ahmad on 18 Mar 2021
@Matt J I think I have checked this in prior but haven't worked. Anyway I got the right answer. Thank you