Matlab fails to access a list of variables

In summary, the conversation involves a person trying to access a list of variables to compare them to a larger matrix A using the eval function. However, there is an error when running it in an m file, even though the variable is present. The person is seeking advice on how to make the function work.
  • #1
MartinV
69
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
  • #2
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?
 

Related to Matlab fails to access a list of variables

1. Why am I getting an error message when trying to access a list of variables in Matlab?

There are a few possible reasons for this error. One common reason is that the list of variables you are trying to access does not exist in the current workspace. Another possibility is that the variables are not stored in the correct format, such as a cell array instead of a regular array. It is also possible that the variables have been cleared or deleted from the workspace. Check your code and make sure the variables you are trying to access are properly defined and stored.

2. How can I troubleshoot and fix Matlab failing to access a list of variables?

The first step in troubleshooting this issue is to carefully check your code and make sure the variables you are trying to access are properly defined. You can also use the "whos" command in Matlab to see a list of all variables in the current workspace and their sizes and types. If the variables you are trying to access are not listed, then they do not exist in the current workspace. If they are listed but have unexpected sizes or types, then there may be an issue with how they were stored. You can use the "clear" command to remove all variables from the workspace and then re-run your code to properly define and store the variables.

3. I have successfully accessed a list of variables in Matlab before, but now it is failing. What could be causing this issue?

There are a few potential reasons why you may have been able to access a list of variables before but are now experiencing issues. One possibility is that you have changed the variable names or overwritten them with new values in your current code. Another possibility is that you are using a different version of Matlab, which may have different syntax or functions for accessing variables. It is also possible that there is a bug in your code that is causing the issue. Double check your code and make sure all variables are properly defined and accessed.

4. Can a corrupted file or data cause Matlab to fail to access a list of variables?

Yes, a corrupted file or data can cause Matlab to fail to access a list of variables. If the variables were saved in a file and that file becomes corrupted, Matlab may not be able to properly read and access the variables. Similarly, if the data used to define the variables is corrupted or contains errors, Matlab may not be able to properly access and use the variables. In these cases, it is best to try and recover or repair the corrupted file or data, or to use a backup file if available.

5. Is there a limit to the number of variables that can be accessed in Matlab?

There is technically no limit to the number of variables that can be accessed in Matlab. However, there may be practical limitations based on the memory and processing power of your computer. If you are trying to access a very large number of variables at once, it may cause Matlab to slow down or even crash. In these cases, it may be helpful to optimize your code or find alternative ways to access and use the variables in smaller batches.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
136
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
581
Back
Top