Matlab fails to access a list of variables

Click For Summary
SUMMARY

The discussion centers on a MATLAB issue where the eval function fails to access a list of defined variables when executed within an m-file. The user, Martin, encounters an 'Undefined function or variable' warning despite the variables being present in the workspace. The provided code attempts to compare these variables against a larger matrix A, but the reliance on eval is problematic. Suggestions include debugging the function to confirm data integrity and reconsidering the use of eval in favor of more robust data handling methods.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with matrix operations in MATLAB
  • Knowledge of the eval function and its implications
  • Basic debugging techniques in MATLAB
NEXT STEPS
  • Explore alternatives to eval in MATLAB for variable access
  • Learn about cell arrays and their advantages for dynamic data storage
  • Investigate MATLAB debugging tools to track variable states
  • Study matrix manipulation techniques in MATLAB for efficient data handling
USEFUL FOR

MATLAB developers, data analysts, and anyone troubleshooting variable access issues in MATLAB scripts.

MartinV
Messages
68
Reaction score
0
So I have a list of variables, defined in my workspace, and I'm trying to access them each in turn to compare them to a larger matrix A. The eval function calls each variable in turn, stores it under temporary matrix B, then compares it to matrix A, does the necessary calculation, and stores it into matrix Z.

Eval, always the capricious function, works if I try to do it manually but fails as I put this into m file and run it. The warning displayed is 'Undefined function or variable 'AAAN'.' even though that variable is there.

This is my code:

function Z = final(A,list)

B = [];
Z = [];
K = log10(exp(1));

for k = 1:length(list)
eval(strcat('B = ',list{k},';'));
for j = 2:length(B)
for i = 2:length(A)
if A(i,1) == B(j,1) && A(i,2) == B(j,2)
Z = [Z; [A(i,1) A(i,2) K/(A(i,3)-B(1,3))]];
end
end
end
end

end

Now I know most people will advise me to just use cell arrays but since dumping all those matrices into a fresh cell array will take me forever, I'm asking if there is a tweak or two to perform in order to get this to work.

Thanks.
Martin
 
Physics news on Phys.org
MartinV:

Say, I don't use MATLAB but would like to see some debugging, i.e., a confirmation that you are actually getting inside your function the data that you think you are getting.

Again, because you say this is an interactive vs. batch thing...you should do all this 'debugging' in batch mode too (or *.m, style or whatever)

can you explicitly show a small sample?

Also, can you forget about the eval for a moment and just show what the data looks like INSIDE the function?
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K