MATLAB Answers

how to use handles to connect app designer and simulink with each other

59 views (last 30 days)
This is the first time using the option to ask the community because i am stuck for two days now. I had to write a rather complex program in stateflow (stateflow runs inside of simulink) and did so successfully. Now i have to create a user interface for easier usage. I have no experience in doing so and after some unsuccsessful tries in GUIDE i switched to app designer. In the startup funtion off the app i simulate the Simulink model "Simulation".
later by changing a value in a constant block i started the actual simulation:
set_param([bdroot '/start simulation'], 'Value', 'true');
1. However after closing the app the Value stays true. Is there any way of only changing the value temporarily without using a reset button inside of the app?
2. To connect the Simulink model with the GUI i have read in multiple threads, that i have to use handles. ( App Designer to control a Simulink Model and read back model data ) However I don't know where to put the code for the handle (do I have to put both into the startup function of the app?) At the moment i try to access the app from a matlab_function inside of simulink (Gewebeinfiltrationsautomat_EXE is the name of the mlapp-file)
function fcn(process_timer, actual_sample_size, finished,)
app_handle = Gewebeinfiltrationsautomat_EXE;
app_handle.lbl_timer.text = num2str(process_timer);
Do i have to create the handle in the startup function of the app? If yes, can I then simply access the handle from any matlab function without problem?
Thank you for your help.

  1 Comment

Bartha Balazs
Bartha Balazs on 30 Mar 2018
Hi, did you found any solution to your problem ? I'm struggling with a similar issue so if you have any solution peas let me know :)

Sign in to comment.

Accepted Answer

Philipp Küsters
Philipp Küsters on 1 Apr 2018
I have. You can access public app designer functions with coder.extrinsic. You first have to create the functions in App Designer. Then you create a handle in Simulink/Stateflow. My initialise block looks like this:
% initialise app and import the apps interface
en: coder.extrinsic('gia_app', 'update_clock', 'update_sample1', 'update_sample2', 'update_position1', 'update_position2' ,'update_display', 'update_finished'); %'update_temp9', 'update_temp10', 'update_temp11')
app = gia_app();
(This is written in a stateflow chart. I do not know how to implement it in Simulink without the use of Stateflow.)
-"app" is the handle i create -"gia_app" is the name of my app designer file -everything except "gia_app" behind "coder.extrinsic" is a public function in the app designer file
The functions can now be called in Stateflow. example:
update_display(app, message)
You can even pass variables, like in this case message. The public function "update_display" has exactly one input argument. This is the app designer code of the function:
function results = update_display(app, input)
% 0 = start
% 1 = change pos of sample1
% 2 = change pos of sample2
% 3 = spectate sample1
% 4 = spectate sample2
% 5 = mix
% 6 = waiting
if input == 0
new_line = 'Prozessstart';
Important Note: If you want to pass variables from App Designer to the running Simulink-Simulation, you will probably save them in the Matlab base-workspace. Simulink does not update the Variables automatically, while the simulation is running!!! Whenever you change a variable in App Designer or generally somewhere outside of the running Simulink-Simulation, you have to pause, update and continue the simulation. (This will not notice the pausing, this is all done in very short time). example:
set_param('gia_sim' ,'SimulationCommand','pause'); % pause simulation, so variables can be read
assignin('base', 'pres11', value);
set_param('gia_sim','SimulationCommand','update'); % update app with new variable
Hope I could help you. If you have more questions I would be happy to help. Im pretty busy myself right now though, so it could take some time.


Eric Hannouz
Eric Hannouz on 8 Apr 2018
Hello Philipp, What Type did you use for the StateFlow Local variable "app" ? If you use an Expression like "gia_app", don't you have a compilation error of the kind "Expression 'gia_app' for type of data 'app' did not evaluate to a valid type" ?
Philipp Küsters
Philipp Küsters on 10 Apr 2018
Hi Eric, I used a local Variable of size -1 with inherited complexity and no start value. Make sure your files are in the same folder. I don't get any error messages. Here's a snap of my chart's top layer.
Good luck with your project.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!