Run a .mlapp script from a .m script with input variables and output variables

I have created an app using the app designer and the .mlapp script works as I want it when I run it from within the app designer specifying the input variable from the run icon. I am yet to work out how to get the app work similar to a function with it accepting input variables and outputting variables.
I have tried calling the mlapp script from within the .m script by script_name(input) but it doesn't accept the input variables in this way. I have tried specifying the input variable as a global variable and using the run function but neither of these seem to pass the input variable into the app's code. I have also tried some of the options for outputing variable from the app. I tried the assignin function but I can't tell if this is working without getting it to run properly in the .m script. I know I could get it to work with save and load functions but this seems clunky. Ideally I would like the app to run as a function with all the same functionality as the app designer.
I have read online that you could change the classdef to a function. If anyone has any documentation on how to do this or an example that would be helpful.

 Respuesta aceptada

You can call your .mlapp with input argtuments.
Please see "Define Input App Arguments" section. You need to change Code Options so that it allows input arguments.

5 comentarios

I have done this but how do I then run the app from a .m script with an input variable from the workspace?
You just pass the input from workspace variable.
For example, if you have the following mlapp, with public property count and startupFcn with input argument in1,
app1.mlapp
properties (Access = public)
count=0
end
% Code that executes after component creation
function startupFcn(app, in1)
app.count = in1;
%uialert(app.UIFigure, num2str(in1), 'Input', 'Icon', 'info')
end
% Button pushed function: Button
function ButtonPushed(app, event)
app.count = app.count + 1;
end
You can call this mlapp in the following m script.
callMyApp.m
in1 = 5;
% Call app1.mlapp with input argument
myapp = app1(in1);
% Push the button twice
myapp.Button.ButtonPushedFcn(myapp, matlab.ui.eventdata.ButtonPushedData)
myapp.Button.ButtonPushedFcn(myapp, matlab.ui.eventdata.ButtonPushedData)
% Disp the counter the value would be 7
myapp.count
Result
How do I ask my script to wait until some action has been completed in the app before returning myapp.count?
I have worked out how to do it using the waitfor function.
myapp = CropControls(frame);
waitfor(myapp,'finished',true)
RectPosition = round(myapp.UIOutput);
myapp.delete
Great!! I also thought of waitfor.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Productos

Versión

R2023a

Preguntada:

el 9 de Feb. de 2024

Comentada:

el 13 de Feb. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by