Run Simulink from Matlab function - matlab

I am running Simulink using FastRestart, as I need to start and stop the simulation multiple times changing parameters. When I run Simulink from the main script, there are no problems. However, as soon as I make the script a function so that I can run it for different input data, I get an error that is clearly related to Simulink not seeing the Matlab workspace within the function.
To be more precise, say sfile is my Simulink file, then I run the following lines AFTER having initialized all variables I need in structures in Matlab:
load_system(sfile);
set_param(sfile,'FastRestart','on');
set_param(sfile,'SaveFinalState','on');
set_param(sfile,'SaveCompleteFinalSimState','on');
set_param(sfile,'SimulationCommand','update');
At the last line, I get the error that Simulink does not recognize mdl.tStep (which is the time step), as mdl is not a recognized structure. In fact, it is and if I run Simulink from the main script everything is fine.
Now, in the past, I would have used
options = simset('SrcWorkspace','current');
However, an expert I know has advised me against simset (as it may get deprecated in the future) and encouraged me to use set_param instead. I have
looked up the options for set_param on-line, but I could not find the setting for the Matlab workspace.
Any help would be greatly appreciated. Thank you in advance!

In many instances it is better to use the Model Workspace rather than the Base Workspace:
hws = get_param(model, 'modelworkspace');
hws.assignin('mdl',mdl);
At least be aware that this option exists.

A solution to your problem might be to use the assignin-function to all the variable whose value you want to pass to simulink in your matlab base workspace. To do so just use
assignin('base','mdl',mdl)
Having the variable in your base workspace should allow simulink to see it.

Related

Alternative to 'evalin' with MATLAB App Designer

I'm creating an app that I eventually want to compile as a standalone application. The app constructs a GUI for a preexisting MATLAB program and I utilize the evalin function twice in the following manner.
First, I run a 'start' function as follows
evalin("base",'func.start')
This instantiates a number of variables which are necessary to run the GUI.
Next, I run the following code to read in one of the variables into the AppDesigner workspace.
fx = evalin("base",'fx')
I wanted to see if there was a deployable alternative to 'evalin' that could be compiled by the MATLAB Compiler. Any help would be greatly appreciated!

MATLAB Figures and Global Scope

I am using functions from a MATLAB package ("EEGLAB"). One of the functions I'm using ("pop_selectcomp") creates a GUI. However, when I try to interact with the GUI, an error results: an expected variable (In this case "EEG", a data structure) is not defined. That's odd, because pop_selectcomp has EEG as an input. I discovered that declaring global EEG anywhere in the function stack above my call to pop_selectcomp makes EEG available to it again. This is the structure of my function stack.
Main Script
data import function loads variable "EEG"
function to process data
function call to pop_selectcomp
So declaring EEG as a global in the main script or the data processing function fixes the problem.
My interpretation is that when pop_selectcomp creates its GUI window, it's being created outside of my function stack in the main workspace or something like that. So, the EEG variable is only available to it if it's been declared global above the function call. I'm not very familiar with Matlab figures and GUIs, but I guess that normally pop_selectcomps doesn't have this problem because it's not called as a sub-function.
Is there any better way to make this work? Can I somehow point pop_selectcomps' GUI at the correct sub-workspace where it will find the variables it needs? I can modify pop_selectcomps if I have to, although that would be messier. The function can be found here:
https://sccn.ucsd.edu/svn/software/eeglab/functions/popfunc/pop_selectcomps.m

Impossible to tune Simulink Parameter at simulation time

I have a problem. I have an embedded function in my simulink model which has a structure (struct) as parameter. It contains only numerical values and I generate an S-Function of the embedded function by right clicking on the block and C/C++ code --> Generate S-function.
I then have the compiled block, if I try to change some values of my struct nothing changes (the fields of my struct stay the same as when I first compiled my embedded function).
When I compiled the embedded function block I selected the parameter to be tunable. I selected the parameter to be tunable in the Model Explorer. I tried to follow this video tutorial by mathworks: http://fr.mathworks.com/videos/tunable-structure-parameters-68947.html (The video is for r2010a while I am at r2015b) It is a bit different the interface in r2015b (from the one in the video) but when I click on Configure , like the guy does in the video, nothing happens.
Could you help me please?
Thanks a lot.
Once I had also decided to reduce the number of tunable parameters by checking the 'inline parameters' checkbox and then specifying the exception variables (variable that have the permission to be tunable even when 'inline parameters' is turned on. It did not work.
In case you aim does not heavily rely on optimization, it would be better if you simply turn off 'inline parameters'.
After that, the constant blocks (i suppose you are giving the input to your s-function from constant blocks) will become tunable.
Another advice: add mex in the init function of your model callbacks. It will save you from getting weird output (usually due to uncleared/un-reset variables from previous runs).
Hope it helps!

Save array to base workspace from simulink model

I'm using a MATLAB function block within a Simulink model. I build this model and run it on a dspace system with 1 kHz. To evaluate my experiment I need the data (20x20 double array) that is calculated in my MATLAB function block. Is it possible to export the data to the base workspace?
To read a variable from your system, the easiest way to do so is using ControlDesk. Create a project and download/start your experiment using ControlDesk, then it is automatically aware of the running application and can read the variable. You now have to configure a Measurement (or Capture in old versions) and export the results to MAT. You can find detailed instructions in the documentation from dSPACE, called HelpDesk.
Alternatively you can use the XIL-API or HIL-API to automate the above steps.

Get sys for save_system simulink

Disclaimer: I'm not a professional simulink/matlab programmer
If I've opened simulink and created a model, then i want to save it from the commandline in matlab. The save_system works, but I also want to specify a filename, for this i need to use save_system(sys, newsysname.slx)
I can't seem to find a function to get sys. How can I get the same sys as save_system() uses?
Use gcs to get the current system or bdroot to get the top-level of the model. find_system may also be useful.