To Workspace block does not return any result when I simulate the modelo from App Designer

Hello,
I am developing a simple User Interface with App Designer to control a Simulink Model. The problem is that excuting the sim () function, the simulink models starts simulating but It does not send the results to Matlab Workspace. If I execute the model directly from Simulink there is no problem.
Is there any function that returns "To workspace" data when executing from App Designer?
Thank you!

Answers (2)

Hi Maria,
Yes it is possible to send the simulation data to workspace from inside the appdesigner. For that use a 'to workspace' block in your simulation model. Then in your appdesigner, you can use assignin function to assign the output to any workspace variable.
For example - simout = sim('SineWave_SL');
assignin('base', 'Data_Time', simout.SineWaveValue);
In the above case 'SineWave_SL' is the name of my model. The model has a sine wave whose output is connected to the 'to workspace' block. The assignin function takes workspace name, variable name, data as arguments. In the above case, the workspace is 'base', 'Data_Time' is the varible and simout.SineWaveValue(where SineWaveValue is my 'to workspace' block name in the model) is the data.
You can refer to this link to know more about assignin function -: https://www.mathworks.com/help/matlab/ref/assignin.html
I hope this solves your problem.

6 Comments

Hi!
I've tried what you say and applying the assignin fuction gives me this error:
Dot indexing is not supported for variables of this type.
The code used is shown below:
simout=sim('inverter');
assignin('base', 'x',simout.current);
Where "inverter" is the name of my simulink model, "x" the name of the variable and "current" is the name of "To Workspace" block.
Thank you!
Ok, so for debugging purpose, first try running the simulink model with the 'to workspace' block independently and take a look at the output in the base workspace.
Also run the simout('inverter') command from the MATLAB command line to check what exactly are we getting as the output of model. Because to debug, it is important to understand the nature of the output of your workflow.
If possible, share your model so that i can look into it.
I have tried it and still is not working. Here I share a simplified model:
  • In the Simulink model (counterCondition.slx) there are 2 "To Workspace" blocks and when I run the model from Simulink it saves correctly the results in workspace
  • in the App Designer model (Applast.mlapp) there is just a botton and I want to run the simulation when I press it.
Thank your for your time!
Use this in your app button callback function-
simout=sim('counterCondition');
assignin('base','x',CounterVariable);
assignin('base','y',CounterVariable2);
You don't need to use simout.CounterVariable because if you carefully observe by running the simulation normally, the output is simply the name of your workspace variables and the output is not nested. That's the reason it is always beneficial to observe the outputs nature. Try it this way, i think it should work fine

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!