MATLAB How can you use the Matlab eval function in this scenario?

AI Thread Summary
The discussion revolves around a MATLAB code issue where the user encounters an error due to the use of the `eval` function to dynamically access variables named 'G1', 'G2', etc. The error message indicates that 'G1' is undefined. The user seeks clarification on why an alternative method using string concatenation to create variable names does not work. Another participant suggests that using an array might be a better approach, especially since the user plans to use the resulting variable in a dendrogram function. The user expresses a desire for alternatives to `eval`, indicating a need for more efficient coding practices in MATLAB, particularly as they are still learning the language.
mashiling
Messages
2
Reaction score
0
Dear Friends/Experts,

I have below code for execution but got error. Any anyone pin point and correct me please?

function H = autclus

for i = 1:500
V = eval(['G' int2str(i)]);
end

end


? Error using ==> eval
Undefined function or variable 'G1'.

Error in ==> autclus at 10
V = eval(['G' int2str(i)]);
 
Physics news on Phys.org
Why can't you use like this

for i=1:500

v{i}=strcat('G',int2str(i));

end
 
n.karthick said:
Why can't you use like this

for i=1:500

v{i}=strcat('G',int2str(i));

end

Hi,

Thank you very much for the reply.
If it's using array, I have a little problem here as i will subsequently use this function.

H = dendrogram(Z, 'labels', V);

maybe this is the reason i attempted for eval. any alternative? mate.. :)
Im still a rookie in matlab
 
Back
Top