AppDesigner, Executing .m files via Pushbutton does not work

Hello Community,
I have problems connecting a GUI to MATLAB, especially the base workspace.
I have an App Designer GUI, which executes three .m-files if a pushbutton is pressed.
function INITIALIZEButtonPushed(app, event)
119 Initialization
120 Create_ACCCycle
121 helperACCSetUp
end
That works fine and the variables to be initialized can all be found in my Matlab workspace.
All of them stay there - also until a breakpoint set in line 42 of Create_ACCCycle.m including a variable called controller_type (possible values are [1;2] based on the Adaptive Cruise Control example in Automated Driving Toolbox)
But when in line 42 of my Create_ACCCycle.m code Simulink Model shall be executed a get a weird error from the Pushbutton:
When I initialized all my variables directly in MATLAB everything works fine, only if I execute these scripts via the Pushbutton in the GUI I get this error. I also cannot understand why 'controller_type' does not exist according to the error. As said, at my breakpoint in line 42 so directly before starting my simulink-Model in line 42 the variable 'controller_type' is still set (to 1) in the MATLAB workspace on the right.
Does anyone have an idea why the Pushbutton is still relevant in this case? I mean, In my opinion the pushbutton should have done his job after executing the scripts. The pushbutton mustn't have any logical connection to anything later done in my code.
Error using Create_ACCCycle (line 42)
Error due to multiple causes.
Error in Eingabepanel/INITIALIZEButtonPushed (line 120)
Create_ACCCycle
Caused by:
Error using Create_ACCCycle (line 42)
Variant control 'controller_type == 1' used by block 'ACCWithSensorFusionMdlRef/Adaptive Cruise Controller'
should return a logical value.
Error using Create_ACCCycle (line 42)
Unrecognized function or variable 'controller_type'.
Error using Create_ACCCycle (line 42)
Variable 'controller_type' does not exist.
Error using Create_ACCCycle (line 42)
Variant control 'controller_type == 2' used by block 'ACCWithSensorFusionMdlRef/Adaptive Cruise Controller'
should return a logical value.
Error using Create_ACCCycle (line 42)
Unrecognized function or variable 'controller_type'.
Error using Create_ACCCycle (line 42)
Variable 'controller_type' does not exist.
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 382)
Error while evaluating Button PrivateButtonPushedFcn.

6 Comments

If you're using assignin, that doesn't mean that called function can use the variable from the workspace. Pass the variables or properties directly in the call of function.
Hello Mario Malic,
would you mind giving a more concrete example where to put my variables (or scripts) in order to execute properly. How can I make sure that they are passed in the "call of function".
Meanwhile, I get a similar error which I guess is liked to the probably bad code I have (see errors at the bottom).
For me this is a mystery, I don't get why my Simulink "createBus" and "AssignmentThreshold" has anything to do with the simple GUI I try to set up.
I've read a lot about handles in this forum, but they seem to address the way GUIs were built up years ago without the AppDesigner. I only have the parameters "app" and "event" which are greyed out - which of course will make sense even if I don't get why.
EXAMPLE FROM THE FORUM:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
evalin('base','script')
Maybe you or someone else can give more advice on these issues. Thank you very much !!
MY LATEST ERROR MESSAGES:
Error using matlabshared.tracking.internal.SimulinkBusUtilities/blockConstructor
Unable to resolve expression 'assigThresh' for parameter 'AssignmentThreshold'.
Error in matlabshared.tracking.internal.SimulinkBusUtilities.createBus
Error in helperACCSetUp (line 106)
multiObjectTracker.createBus(blk{1});
Error in Eingabepanel/INITIALIZEButtonPushed (line 120)
helperACCSetUp
Caused by:
Error using slResolve
Cannot resolve: assigThresh.
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 382)
Error while evaluating Button PrivateButtonPushedFcn.
function [Output_Arg1, Output_Arg2] = My_Function(Input_Arg1, Input_Arg2)
% ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
% What receives the workspace What My_Function has access to
% that function has been called in
Initialization
Create_ACCCycle
helperACCSetUp
If Initialization generated some values that Create_ACCCycle uses, you're not passing it through
[arg1, arg2] = Initialization
Create_ACCCycle(arg1, arg2)
Otherwise, use properties, create them at the start of code appropriately and you can pass the 'app' as an argument to each function, and within it refer to the properties as
app.Property1
Regarding the error: typo? assigThresh
Hello Mario Malic,
thank you once again for your hints. I pretty much appreciate your instant responses !
Your approach might be good if I know all my variables and their datatypes as it is the case for my "Initialization" script. But it hardly works for the scripts Create_ACCCycle and helperACCSetup as their In-and Outputs mostly are busses and parameters from a MATLAB toolbox which I hardly understand (such als RadarGenerator Setups in Adapative Cruise Control) and code parts like these below which I have no idea how to generally store and pass the outputs properly.
Is there no possiblity to simply start these scripts from the GUI but having no other interaction execept of maybe plotting results in the end?
% Create the bus of tracks (output from referenced model)
refModel = 'ACCWithSensorFusionMdlRef';
wasReModelLoaded = bdIsLoaded(refModel);
if ~wasReModelLoaded
load_system(refModel)
blk=find_system(refModel,'System','multiObjectTracker');
multiObjectTracker.createBus(blk{1});
close_system(refModel)
else
blk=find_system(refModel,'System','multiObjectTracker');
multiObjectTracker.createBus(blk{1});
end
if ~wasModelLoaded
close_system(modelName)
end
I guess I meanwhile have found what I was looking for:
evalin('base', 'name_of_script')
https://www.mathworks.com/matlabcentral/answers/270019-how-do-i-run-a-script-from-a-gui-that-will-run-in-the-base-workspace
You can generate property without specifying its type.
properties (Access = public)
X
Y
end
In the first post, you were missing a controller_type variable, which is variable from initialization function that Create_ACCCycle does not have access to. You can set it as a property and refer to it as mentioned above to check whether is that the only thing shared between functions.
You don't have to pass everything, just what the function needs. I prefer to work with properties, since you don't have to worry a lot. Otherwise, a typical approach would be
controller_type = Initialization
Create_ACCCycle(controller_type) % and so on
Edit: I forgot to mention that I did not work with Simulink.
Great find on the solution!

Sign in to comment.

Answers (0)

Categories

Products

Release

R2020a

Asked:

on 27 Sep 2020

Edited:

on 28 Sep 2020

Community Treasure Hunt

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

Start Hunting!