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

  • Context: MATLAB 
  • Thread starter Thread starter mashiling
  • Start date Start date
  • Tags Tags
    Error Function Matlab
Click For Summary
SUMMARY

The discussion revolves around the use of the Matlab eval function to dynamically reference variables named 'G1', 'G2', ..., 'G500'. The user encounters an error indicating that the variable 'G1' is undefined. An alternative approach suggested involves using cell arrays to store the variable names, which can then be accessed without the need for eval. This method enhances code readability and reduces the risk of runtime errors associated with undefined variables.

PREREQUISITES
  • Understanding of Matlab programming syntax
  • Familiarity with cell arrays in Matlab
  • Knowledge of the dendrogram function in Matlab
  • Basic concepts of variable scope and dynamic variable referencing
NEXT STEPS
  • Learn about Matlab cell arrays and their usage
  • Explore the strcat function for string manipulation in Matlab
  • Investigate alternatives to eval for dynamic variable access
  • Study the dendrogram function and its parameters in Matlab
USEFUL FOR

Matlab programmers, data analysts, and anyone looking to improve their coding practices by avoiding the use of eval for dynamic variable referencing.

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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
2K