matlab model -- access base workspace variables - matlab

I am running my test.m file to create variables in my base work space. This is the content of my test.m file :--
a=10; % define a variable with particular value
b=20; % define a variable with particular value
c=0; % define a variable with particular value
In my MATLAB model now i am trying to access the variables a & b. But it is returning me the value zero for both a & b.
I am using a function call generator to trigger a model every 10 msec.
Solver type : Fixed Step
Solver : Discreate(No Continous states)
Why i am not able to access the workspace variables in my simulink model.
Please see the attached print shot.

You are using an Inport and you named it with the variable name but it does not affect the variable to it, it is only a port. Try using the Constant block and set it to your variable name ("a" for example)

Related

From workspace to Simulink

I want to import one matrix 5x5 from Workspace to Simulink. The software gives me an error: "Invalid workspace variable specified as workspace input in 'MODELLO_From Workspace'. Time values must be non-decreasing." How can I solve the problem?
You can use constant block for that.
Here First I created a variable as sample_matris (5x5) in workspace.
sample_matris = rand(5,5);
Here is the values created in workspace.
Then I used constant and entered name of the variable defined in workspace.
You can see that I can use values defined in workspace, by using constant block in simulink. If you need specific indices of the matris, you can use selector block in Simulink.

How can I create my own parameters or attributes for a block in simulink?

In this case I am trying to create a new block parameter as a new property to save a specific new data which I don't want to be saved in a defalut parameter which is already reserved for other diffrent related datas
for this parameter I want to use the command get_param and set_para that need to be alreday existed
I mean with default parameters , those .
https://edoras.sdsu.edu/doc/matlab/toolbox/simulink/slref/parameters2.html#7515
Programmatically create masks
I'm not sure this is exactly what you are searching for, but I made an example on how to create programmatically a mask via script in MATLAB/Simulink. I will not use get_param/set_param, even if it is possible to obtain the same results using those commands. We will go with the Simulink object that is simpler and more clear (at least IMHO).
For our playground lets create this simple subsystem (block) with a simple constant that gives in output the name of a variable named a that we want to take from the mask:
Look at the address of this block. My simulink model is mask.slx, thus I can address this subgroup with the address mask/block (upper left corner of the viewport), as you can see here:
At this point we can use the following code to add an edit parameter box for the subgroup, which fixes the value of a:
clc
clear all
% The subgroup for which we want to programmatically create a mask
block_name = 'mask/block';
% Now we can create the mask parameter programmatically as you requested
% There are two way: the old one using get_param and set_param and a more
% clear one using the Simulink interface.
% I will go with thw second one, since it is really more straightforward
% with respect to the first one.
% The first think to do is to create the mask itself
% If the mask already exist, we would get an error, thus we can avoid it by
% checking if it already exist. This is something that you should check out.
mask_hdl = Simulink.Mask.create(block_name);
% mask_hdl = Simulink.Mask.get(block_name); % For use an existing mask
% Now we are ready to create the mask parameters:
edit_a_hdl = mask_hdl.addParameter( ...
'Type', 'edit', ...
'Prompt', 'Sets constant variable', ...
'Name', 'a');
edit_a_hdl.Value = '10';
Running the script the code will be masked and the variable will be set, as you can see here:
There are more information on this topic here.
Setup parameters programmatically for masked Block
Now lets say you have the playground as before done and you have the subgroup masked as in the last image. You can set its value in the mask programmatically (or get it) through the get_param and set_param as follows:
value = get_param(block_name, 'a');
value = str2double(value); % Values should always be string!
% Thus we must convert it
set_param(block_name, 'a', sprintf('%d', value * 100));
and as you can see the value has now been updated:
Again, you can achieve the same result by using the Simulink object.
mask_hdl = Simulink.Mask.get(block_name);
edit_a_hdl = mask_hdl.Parameters(1); % We know its position in the struct array
value = str2double(edit_a_hdl.Value);
value = value * pi;
edit_a_hdl.Value = sprintf('%f', value);
and, as you can see, we have our new value:
Simulink blocks in many toolboxes are created using MATLAB System objects. If you want to create a new parameter for an existing Simulink block, you may want to create a public property in the shipped System object code. If you are creating your own Simulink block, then writing your code in MATLAB system objects will be very friendly to change/create parameters as you wish.
Simulink extension system object can be created as follows:
To create a Simulink block from a System object, create "MATLAB system" block from the existing Simulink blocks and call your system object from the MATLAB system.
All public properties in the system object code are visible in Simulink mask dialog as shown in below picture.
Hope this is what you are looking for.

using global variable in other functions in matlab [duplicate]

Is there a way to declare global variables in MATLAB?
Please don't respond with:
global x y z;
Because I can also read the help files.
I've declared a global variable, x, and then done something like this:
function[x] = test()
global x;
test1();
end
Where the function test1() is defined as:
function test1()
x = 5;
end
When I run test(), my output is x = []. Is there a way I can make it output the x=5, or whatever I define x to be in a separate function? In C, this would be an external variable, and I thought making it a global variable should accomplish just that.
You need to declare x as a global variable in every scope (i.e. function/workspace) that you want it to be shared across. So, you need to write test1 as:
function test1()
global x;
x = 5;
end
Referring to your comment towards gnovice using a global variable can be an approach to solve your issue, but it's not a commonly used.
First of all make sure that your .m files are functions and not scripts. Scripts share a common workspace, making it easy to unwillingly overwrite your variables. In contrast, functions have their own scope.
Use xUnit in order to generate repeatable unit test for your functions. By testing each function involved in your program you will track down the error source. Having your unit test in place, further code modifications, can be easily verified.
A possible way to get around the global mess is to assign the variable as appdata. You can use the functions setappdata and getappdata to assign and retrieve appdata from a MATLAB window. As long as a MATLAB session is active there exists a window denoted by 0.
>> setappdata(0,'x',10) % 0 indicates the root MATLAB window
Now the variable x is not visible to any script or function but can be accessed wherever needed by using getappdata.
function test
globalX = getappdata(0,'x');
disp(globalX);
end
x =
10
The good news is that you can assign any valid MATLAB object to appdata, just be cautious with the names, using unique names for appdata fields like ModelOptimizerOptions instead of a generic x,y would help. This works on compiled executables and code deployed on the MATLAB production server as well.

Simulink: 'To workspace' for a scalar value

I would like to export one scalar variable from a Simulink Diagram to the Matlab Workspace.
Although I know that the value of 'Chemin' can be changed during the simulation, I am only interested in exporting the initial value to the workspace; I do not want a TimeSerie variable (like the 'To Workspace' block would do), I only one want scalar value.
Thank you in advance for your help!
I don't think you can do what you want. Everything in Simulink is time-based so you have to save the entire variable as a function of time to the workspace. However, you can add a model callback in StopFcn that extracts just the first value and clears the time-dependent variable from the workspace, e.g.:
chemin_0 = chemin(1); % assuming chemin is the name of the time-dependent variable saved to workspace
clear chemin
The StopFcn callback is executed after the simulation stops.
You can do this pretty easily with an Enabled Subsystem. Make the enable signal false at all times except t=0 by using a constant (=0) and an Initial Condition block (=1), as per the following picture.
Inside the Enabled Subsystem have
with the save format set to be Array. The simout variable will then be a scalar valued number.

How do I read a matrix from workspace in simulink?

I need to read a matrix variable from the workspace in Simulink. I am using the simin block but I have a problem with this. It shows me the following error:
Invalid workspace variable specified as workspace input in
'untitled1/From Workspace'. Time values must be monotonically
increasing.
Which properties of this block (simin) or the matrix variable in workspace should I change to read it in simulink with the block correctly and operate with it (Multiply, Transpose....)?
what does your input variable look like? I use a structure:
variable1.time=[0 1 2];
variable1.signals.values=rand(1,3);