Undefined variable when running appdesigner code

1 visualización (últimos 30 días)
Ashton Linney
Ashton Linney el 5 de Abr. de 2020
Comentada: Ashton Linney el 5 de Abr. de 2020
I have some code that works in normal matlab:
if app.Maint_Select.Value == "Seal Coat"
mat = xlsread('Maint.xlsx','B3:Q6');
elseif app.Maint_Select.Value == "Slurry Seal"
mat = xlsread('Maint.xlsx','B9:Q12');
elseif app.Maint_Select.Value == "Overlay"
mat = xlsread('Maint.xlsx','B15:Q18');
end
for k = 1:size(mat,1)
vec = mat(k,:);
A = reshape(vec,[4,4]);
assignin('base',strcat('MAT',num2str(k)),A);
end
%===Defines identity matrix===
I = eye(4);
%===Uses Kronecker Tensor Product to generate all combinations rate matrix for maintenance===
maint_mat=full(KronProd({MAT1,I,I,I})) + full(KronProd({I,MAT2,I,I})) + ...
full(KronProd({I,I,MAT3,I})) + full(KronProd({I,I,I,MAT4})) ;
After pasting this into the code for appdesigner and running it for my app, it comes up with the error saying:
" Undefined function or variable 'MAT1'. " on line 16 of code.
However, 'MAT1' has been defined on line 11 of the code.
Does anyone know why this is happening and how to fix it?
Thank you

Respuesta aceptada

Image Analyst
Image Analyst el 5 de Abr. de 2020
Variables only can be seen within a certain scope. You pasted that code snippet into some function, but line 11 (where you say MAT1 was defined) is not in the scope of the function where you pasted the snippet.
If you put this
whos MAT1
after your assignin() for loop but before the "maint_mat=" line, what does it show in the command window?
Also, you should never have to use functions like assignin(), evalin(), eval(), etc. if you do your programming correctly.
  3 comentarios
Image Analyst
Image Analyst el 5 de Abr. de 2020
whos was never intended to "fix" the situation. It was just a diagnostic to see if MAT1 ever got pulled in to your function's workspace.
See, this PROVES that your call to assignin(), to yank that variable in from the base workspace, didn't work. MAT1 does not exist in your base workspace. Anyway, poofing variables into your local workspace by using assignin, rather than passing them in via the argument list is bad practice. Why do you think MAT1 should have been in the base workspace, even though it's not? Another option is to just read in the MAT1 variable using load() or however you (think) you got it into the base workspace.
You do know what MATLAB and all programming languages have different workspaces / scopes, right? So a workspace that you have while in a script ( a simple m-file with no functions) is the base workspace, while a function, or different functions, will have it's own internal workspace just internal to it. And the different workspaces are all separate from each other. One function's workspace can't see variables in another functions workspace, or the base workspace, or the global workspace (yet another one I haven't talked about), unless you do something special to let those variables be seen outside the workspace that they live in. See the FAQ.
Ashton Linney
Ashton Linney el 5 de Abr. de 2020
Thank you for taking the time to reply, I have done some reading into it and understand now. I have also used a different method to assignin now which has managed to solve the problem. Thank you again :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by