MATLAB: Problem with eval notation

evalevil

I have a question about eval in this script
function sort=sortstructure(strucvector, field)
if isfield(strucvector,field)
for i=1:length(strucvector)-1
low=i
for j=i+1:length(strucvector)
if eval(['strucvector(' num2str(j) ' ). ' field])<…
eval(['strucvector(' num2str(low) ' ). ' field])
low=j
end
end
temp=strucvector(i)
strucvector(i)=strucvector(low)
strucvector(low)=temp
end
else disp('wrong field')
end
sort=strucvector
Why cab't I write the part with eval like this:
if eval(['strucvector(num2str(j)).''field'])<…
eval(['strucvector(num2str(low)).''field'])
I know eval is often considered unhandy but it is part of curriculum

Best Answer

I don't see any reason for eval above. If you get rid of it, it should still run. If you're required by law of the professor to use it, have it do something useless and not dangerous at the end.
E.g.
%the rest of the code
eval('disp(''we did it!'')')