Reading a value from matlab script with app designer

33 visualizaciones (últimos 30 días)
angel morenilla perez
angel morenilla perez el 20 de Jun. de 2022
Editada: Nivedita el 14 de Nov. de 2023
Hello,
I would like to know how to read the value of variable stored in a matlab script with app designer so I can use that value to design my app.
Thanks,
Ángel.
  1 comentario
Geoff Hayes
Geoff Hayes el 20 de Jun. de 2022
@angel morenilla perez - please clarify the relationship between the script and the app. Does your app call the script? Is the value/variable calculated in the body of the script and you want to return it to the app? If this is the case, then I recommend that you convert your script to a function so that it returns, as an output parameter, the value that you want the app to be aware of.

Iniciar sesión para comentar.

Respuestas (1)

Nivedita
Nivedita el 14 de Nov. de 2023
Editada: Nivedita el 14 de Nov. de 2023
Hello Angel,
I understand that you want to share data between your MATLAB script and App Designer. There are a few ways in which this can be achieved, you can choose to follow any of them according to your requirement. Here's a brief guide on how you can do it.
1. Using a MAT file:
% MATLAB script
myVariable = 42; % or whatever value
save('myVariable.mat', 'myVariable');
% App Designer Code
function startupFcn(app)
load('myVariable.mat', 'myVariable');
app.myVariable = myVariable;
end
This will load the variable into your app when it starts up. You can then use "app.myVariable" throughout your app.
2. Using Global Variables:
% MATLAB script
global myVariable;
myVariable = 42; % or whatever value
% App Designer Code
function startupFcn(app)
global myVariable;
app.myVariable = myVariable;
end
Again, you can use "app.myVariable" throughout your app.
Please note that using global variables is generally not recommended because it can make your code harder to understand and debug. However, for simple cases, it might be a suitable solution.
Remember to run your MATLAB script before starting your app, so the variable is created and its value is set.
3. Using "evalin" function:
myVar = evalin('base', 'myVar');
This function allows you to evaluate an expression in a specified workspace, such as the base workspace where your script variables are stored. This will retrieve the value of "myVar" from the base workspace and store it in the "myVar" variable in your App Designer code. You can then use this value to design your app as needed.
Note that using "evalin" can be risky if you're not careful, as it can execute arbitrary code in the specified workspace. Make sure to only use it with trusted variables and expressions.
For more information on the "evalin" function, you can refer to the following documentation:
I hope this helped!
Regards,
Nivedita.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by