How do I define variables in simulink model (instead of in workspace)? - matlab

I want to define a few variables in a simulink model. The matlab function block doesn't work because the variables are local. The variables are not input to other blocks, but instead, the variables are parameters to other blocks.
Basically, I want to have a block where I can define a bunch of variables that set the parameters for other blocks. I did this once in the past more than a few years ago now, but I can't find or remember how to do it.
I thought I used a block, but potentially, I set the variables somewhere in model settings or something. I can't remember, and I am not having luck finding it. Any help is much appreciated! I feel this is simple, but I just can't find the solution.

You can do this by defining your data in the model's "Model Workspace", like this:
. There's more about the Model Workspace in the doc. There are various options as to where you can store the parameters.

You can use Simulink data dictionary or model workspace to store the variables/Parameters. go through this link to get more info Usage of the variables/Parameters/Signals for simulink models

Related

Access to "hidden" variables in Simulink

Some example models that are included in the Simulink library, include some variables that their value is not visible. For instance, in the model power_PVarray_grid_det the sample time of the powergui is defined as Ts_Power. I can not figure out the value of this variable. Is there a way to find these hidden variables, have access to them and change their value?
Thanks in advance

Run Simulink from Matlab function

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.

Simulink - How to create independent custom blocks using callback functions

I am currently coding a new library with several models in it (I am used to Matlab, but not to Simulink). I am able to create a model with block parameters, let's say parameter 'p', and a callback function (initfct) which uses this parameter to compute specific values used inside my model (let say a simple gain K=K(p)).
My problem is that my parameter 'p' and 'K' are available directly on the workspace, what I don't want to. Moreover, if I use twice or more this model in a system, the two models share always the same 'K', which also I don't want to.
So how I can make these variables 'p' and 'K' independent when I use my custom model several time, and to prevent these variables to be viewed in the workspace ?
Should I use "Reference models", but I am not familiar with this feature ... ?
Thanks for you answer,
Michael
Within a callback, gcb returns the path to the block which currently executes the callback. Having the path, you can use get_param to access the parameters.
Just for demonstation purposes, insert the following to the MoveFcn of a delay block:
set_param(gcb,'DelayLength',num2str(randi(10)))
It will randomly change the delay whenever the block is moved.
I am not sure if my answer explains everything you need. It might be that you also need a Mask. If you think this answer is incomplete, please update your question and include a small example model demonstrating your problem.
Thanks, with your help I was able to solve the problem.
To be more specific if someone else has the same problem : you need in your mask to declare also internal variables used by the callback function. Unchecked the relevant options so that they not appear as standard input parameters of your model.
My problem was also to use num2str instead of mat2str (when the gain was a matrix acting on multiple inputs).

loading parameter files for data different sets

I need to analyse several sets of data which are associated with different parameter sets (one single set of parameters for each set of data). I'm currently struggling to find a good way to store these parameters such that they are readily available when analysing a specific dataset.
The first thing I tried was saving them in a script file parameters.m in the data directory and load them with run([path_to_data,'/parameters.m']). I understand, however, that this is not good coding practice and it also gave me scoping problems (I think), as changes in parameters.m were not always reflected in my workspace variables. (Workspace variables were only changed after Clear all and rerunning the code.)
A clean solution would be to define a function parameters() in each data directory, but then again I would need to add the directory to the search path. Also I fear I might run into namespace collisions if I don't give the functions unique names. Using unique names is not very practical on the other hand...
Is there a better solution?
So define a struct or cell array called parameters and store it in the data directory it belongs in. I don't know what your parameters look like, but ours might look like this:
parameters.relative_tolerance = 10e-6
parameters.absolute_tolerance = 10e-6
parameters.solver_type = 3
.
.
.
and I can write
save('parameter_file', 'parameters')
or even
save('parameter_file', '-struct', 'parameters', *fieldnames*)
The online help reveals how to use -struct to store fields from a structure as individual variables should that be useful to you.
Once you've got the parameters saved you can load them with the load command.
To sum up: create a variable (most likely a struct or cell array) called parameters and save it in the data directory for the experiment it refers to. You then have all the usual Matlab tools for reading, writing and investigating the parameters as well as the data. I don't see a need for a solution more complicated than this (though your parameters may be complicated themselves).

Pass variable from GUI to function in MATLAB

I have a MATLAB GUI that loads to aid in visually pre-processing data. Essentially it prompts the user to adjust the data range, reduce number of data points, etc... all while providing an updated graph. Upon completion of this work, I want to be able to close out the GUI and pass variables from the GUI to another MATLAB function that does the data analysis. I have found lots of information on how to pass information from a function TO and GUI, but not the other way around.
Any help would be greatly appreciated.
Global variables can cause hard to find bugs. The best solution for your problem (where you want to pass the data directly to another function on close) might be to call the analysis function from the Figure Close Request Function. When the figure your GUI is running in is told to close, it will run the code in this function, which can call your analysis function and have access to the GUI's data.
Matlab GUIs are functions: the code exists in a .m file just like other functions. Like regular functions, they can have return values. You can get as fancy as you want messing with the varargout system, or you can simply return a value, structure, or cell array containing whatever you want. Open up the m-file and edit it to return what you want it to.
Note: If you require special processing when the figure is being closed to generate the appropriate return value, you can reimplement the closeRequestFcn as you see fit.
The easy way: you declare as global variable, where variable stores the data that you want to carry from the GUI to the main MATLAB workspace. Then, you also declare the same global variable on the command window. Hereinafter, variable to be accesible from both scopes, the GUI and the main workspace.
You could also use save or any other alternatives as csvwrite or dlmwrite to store the data into a file, but this doesn't seem to be your case.