Transfer data between App Designer and Matlab (m file)

42 visualizaciones (últimos 30 días)
Tu
Tu el 17 de Abr. de 2024 a las 2:59
Comentada: Voss el 17 de Abr. de 2024 a las 13:31
I create the GUI using App Designer. After that, I send them to WORKSPACE and RUN a script in M-file (TEST) by a line code in App Designer.
I can not get the result in App Designer because the script can not define the variables a_value, b_value despite that they still have on Workspace.
Please help me to fix it. Thank you!

Respuesta aceptada

Voss
Voss el 17 de Abr. de 2024 a las 4:45
Scripts run in the workspace they are called from; that may be the base workspace, but in this case it's the workspace of the function ButtonPushed. Therefore, assigning variables to the base workspace is not needed. You can simply name the variables a_value and b_value instead of a and b. (Also, you'll need to convert the numeric result to text, and set the Text property of app.ResultLabel).
% Button pushed function: Button
function ButtonPushed(app, event)
a_value = app.Spinner.Value;
b_value = app.Spinner_2.Value;
Test;
app.ResultLabel.Text = num2str(c);
end
  2 comentarios
Tu
Tu el 17 de Abr. de 2024 a las 12:19
Thank you very much for your support. I have tried your method and it works well.
Voss
Voss el 17 de Abr. de 2024 a las 13:31
You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

Pratik
Pratik el 17 de Abr. de 2024 a las 4:41
Hi Tu,
As per my understanding, you want to send some data to workspae from the GUI in App Designer and after running the script 'test.m' you get the error of variable not defined.
To access the variables from workspace the function 'evalin' can be used. Please refer to the following code snippet to modify the 'Test.m' file:
a_value=evalin('base','a_value');
b_value=evalin('base', 'b_value');
Please refer to the following documentation for more information about 'evalin' function:
I hope this helps!
  1 comentario
Tu
Tu el 17 de Abr. de 2024 a las 12:18
It's really helpful and I have fixed my problem!

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by