How to make the value in data store memory global - matlab

I have a simulink model that generates signals, I want to store the signal using data store write and data store memory (those are linked together), then I want to be able to use the stored signal (an array) as a global variable in MATLAB.
When I try to make the variable global in matlab nothing happens. Even after I start the simulation
global Signal_full_og; %the variable I want to make global
sig_arr = [];
sim simF.slx %simulation file
for vref= 0:1:10 %1st 10 signals
sig_arr(index, :) = Signal_full_og;
end
Below there is a picture of the Data Store Write and Data Store Memory from my Simulink model
And below there is a picture of the block parameters of Data Store Memory
I have tried to follow the documentation from Mathworks and still I can't get a global variable. Does anyone spot any mistakes in this way or is there a better way to do the same thing?

Related

Use the result values from previous simulation result as guess values for next simulation in Dymola

Initialization could be very cumbersome and easily lead to divergence. A simple strategy is to run the simulation when building a part of the whole system and use the simulation results to modify guess values.
Here is what I got in the PPT from Francesco Casella and the book from Daniel Bouskela.
I found that I could use an option in Dymola as follows, but instead of using the initialization result, I wanna use the result when reaching a steady state. So I'd like to use a python script to extract the result from the .mat result file, then modify the iteration variables automatically. But the key problem is that I don't know when I add more components in my model, the iteration variable set of existing components would change, I don't know what kind of effect would this causes.
Anyone got opinion on this issue, welcome to answer this question.
So my question is where should I find the python
You can use the end values (= steady state) of the simulation result in order to create a new initialization (Dymola Manual 1, section 2.5.12) . If the component names are the same in the sub system model and the total model, you can run the script created in the subsystem model on the larger system model as well. But you have to check if your models have initial equations that hinder an initialization from the outside (see section 4.2 in https://2012.international.conference.modelica.org/proceedings/html/pdf/ecp12076927_KruegerMehlhaseSchmitz.pdf)
It should also be possible to initialize it steady state. Instead of providing initial values for a state x and fixing it, you can provide initial equations for the derivatives such as der(x) = 0;
With that setup activate Save Initial Results and you should be good to go.

Testpoint and its use in Matlab

What is a test point and its intended use in matlab ?
I am working on a model and have to use 3 AND gates in conjunction coupled with similar 2 more AND gates. While checking the model I am getting warning "Identify single logical operator blocks with more than 9 inputs signals.", which is not shown if I use testpoint on each of these AND gate output.
Think of a signal in Simulink as corresponding to a memory location.
In an effort to reduce memory consumption, one of the standard optimizations used by Simulink is to re-use the same memory address when possible.
For instance, assume the input to a gain block is stored at memory location X. Then the output of the gain block would overwrite the data in X. Consequently the input value would no longer be available. But it doesn't need to be as it's value is never used again. (This assumes that the input value is not used elsewhere, such as feeding a block like a Scope.)
In your case, the warning is telling you something about Simulink storing the logical values in memory locations that it subsequently overwrites when possible.
Note that Simulink will never re-use memory when it needs the signal value in subsequent calculations, i.e. when it would effect the simulation result if it did so.
Nor will it re-use memory (for a specific signal) when you designate the signal as being a test point.
This is why the warning is going away in your case.
One particular use of a test point is if you are using a Floating Scope. Floating Scopes cannot be made to look at signals where the memory is being re-used because then it wouldn't be clear which signal was being displaying.
By looking at only test points it is guaranteed that you are looking at the expected data/memory.

How do I input variables into a Simulink model through MATLAB script (SimDriveline)

For my coursework project in MATLAB, I have decided to build a drive-line model within Simulink, using the SimDriveline toolbox. The idea is to get the user to input values for the various parameters that are associated with each part of the model, such as the engine or the transmission. I would like to able to write this in a MATLAB script, but I'm not sure how to assign the values that are input to the Simulink model. For instance, the stock sdl_vehicle example that comes with SimDriveline. I am aware of the sim() command, but I am still confused on how to use it properly.
Also at the end of the simulation, the program is supposed to display the graphs that are collected in the scope window. I know that in the window itself that the scope can be printed to a figure, but is it possible to print that scope to a figure through MATLAB script?
This is the first time I have ever used a program like MATLAB. I would appreciate any help I could get, many thanks in advance!
There is a simulink block called simin:
http://de.mathworks.com/help/simulink/slref/fromworkspace.html?searchHighlight=simin
I used it some days back and it worked quite well. You can use the block in your model and define some signals/varibles as input.
After that you may write a Matlab-Script with an input function to set all the previous defined input values.

S-function storing system time in a variable

What am I trying to do is to save in a variable (global or constant) the system time. I am using a S-function in Simulink. The problem is that when I store the value of the system time in a variable it is continuously incrementing so when I do the difference between the current system time and the time stored in my variable is always 0. What do you think is the solution storing the system time in a variable and what type of variable should I use a global one or a constant. If you have any answer please give me an example because I am new in Matlab.
P.S I am using C language for the S-function.
It sounds as if you are trying to store the system time at the start of the simulation, then during the simulation compare the system time to that stored value. If so, then you should be using anR-Work vector to store the initial system time.
So in mdlInitializeSizes you want
ssSetNumRWork(S, 1);
Then in mdlStart you want
real_T *P_Tinit=ssGetRWork(S);
P_Tinit[0]=((real_T) clock())/CLOCKS_PER_SEC;
Then when you want to use the value use,
real_T itime;
itime=ssGetRWorkValue(S,0);
(The above assumes that you know how to actually get the system time, i.e. include the correct libraries, which from your question it sounds as if you do.)

Matlab/Simulink SimState error

i'm working with Simulink (Matlab) and I have a problem:
I start a simulation and every 'T' time I need to stop it, save the state of the system as Simstate, and then restore the system state by changing a few variables. For example, I am working with the base model 'Inverted Pendulum with Animation' (penddemo.mdl), and when I restore the state I must change the mass of the pendulum, here's some code:
set_param('penddemo','LoadInitialState','on','InitialState','init_state'); % load the initial state
set_param('penddemo','SaveFinalState','on','FinalStateName', 'xFinal','SaveCompleteFinalSimState', 'on'); % save the complete simstate
set_param('penddemo/Pendulum','Mcart','0.600'); % change the mass
sim('penddemo',time); %starting the simulation
The problem is that I get this error:
can not load the Simulink SimState Because The initial model, 'penddemo', was changed after the SimState was saved. Run the simulation again and resave the SimState.
I know that there are limitations in the use of Simstate:
You cannot make any structural changes to the model between the time at which you save the SimState and the time at which you restore the simulation using the SimState. For example, you cannot add or remove a block after saving the SimState without repeating the simulation and saving the new SimState.
but I do not think changing a single variable is a structural change.
Maybe someone knows another way to change some variables before the recovery of Simstate.
Thanks all
I believe that you can only change "tunable" parameters. Try turning "Inline parameters" off, see the documentation on tunable parameters for more details