I have a system in Simulink/HDL coder (see below image please). I have 3 outputs and 3 inputs. I want my system to run 10 times. After each iteration it should select the outputs and use them as inputs. How I can do that?
Build a loop using a memory and a initial value block for each signal. The memory block allows you to access the previous iteration signal and the initial value block is used to set the input for the first iteration.
A simple example looping back one signal can be found here in the documentation
In your case it would look like this:
To get 10 iterations, set your simulation time corresponding. For example a fixed step discrete solver using 1s sample rate and 9s simulation time.
Related
I state that I have searched for a long time but I can't find an answer to my problem.
I don't find a Simulink block that has the function of giving in input one step that is worth an x (defined by me) for 5s and 1.1x from 6 to 50s at two models (one linear and one non linear systems)
Usually i use the Step Block to start an initial and final value of the step but the change is almost instantaneous. What block can I use to do this?
Option 1
Use multiple step blocks and add them up. Simple as that.
Option 2
Use the signal builder block.
As a student I am currently working on a Matlab Simulink project. I am quite new to using Matlab/Simulink (few weeks).
I want to implement and run a Matlab “.m” file with which I can open Simulink and start the simulation. The aim is to do a 24h Test with a load cell cut into 1h “pieces” and to save the data to different sheets of an excel file each hour. So my simulation runs for 1h, stops and starts again, and so on. Through Matlab and a “for” loop I do the measures 24 times.
Between measuring steps I have to wait for simulink to finish its measures and saving the file in order for the Simulink window to be able to get closed by close_system('Thesis_SerDatTransm_Simulink').
So I tried to implement the delay with a while loop and by checking if the measures I get fit into an array of the size bigger than 449 (I measure 449 values):
for k=0:1:24
% Load Simulink
load_system('Thesis_SerDatTransm_Simulink.slx')
% Open Simulink
open_system('Thesis_SerDatTransm_Simulink.slx')
% Start Simulation
set_param('Thesis_SerDatTransm_Simulink', 'SimulationCommand', 'Start');
% Save Data
my_cell = sprintf('A%d',k);
xlswrite('file.xlsx',y,my_cell)
% Wait for Simulation
while 1
test=size(y)>=449;
if (test)
close_system('Thesis_SerDatTransm_Simulink')
break
end
end
end
The Problem now is, that program gets stuck at the while loop. Simulink is started, but no simulation or data gathering is done.
So I wondered if anyone could check if something is wrong with my While loop, since the rest of the programm works all fine without the loop (but receiving an error message, that during the simulation, Simulink window can't be closed).
I know there is a way to create a delay with waitforin matlab and create another function which I could call, but I couldn't figure out how to do this yet.
thanks
Regards
hohmchri
The right way to do this is to use the sim function to run your model (not the sequence of load_system, open_system and set_param that you have).
sim will block the execution of m-code until the model completes executing. Data can either be returned into the workspace (when used with no output arguments) or returned as an output from the call to sim. (And then you can write it to Excel as you've done.)
The only reason not to use sim, and perhaps use the commands you have, is if the model takes a long time to initialize, and you don't want to to open and close it every time through the loop. However, even in this case your code isn't correct. The load_system would be outside the loop; the open_system is not required; in your while loop you would poll the model's SimulationStatus property to see if it is still running (not the size of the y variable); and the close_system would be after the loop (as indicated by #m_power in one of the comments).
As written you should use the matlab pause command. This stops your execution for X seconds.
You should also look to optimize your code as m_power states
I am trying to vary a variable every 10 seconds while a simulation on simulink is running. I defined (Kb+Ks)/N inside some Gain blocks and I want to vary only Kb from its minimum to maximum value and back to its minimum value during simulation. I have tried using set_param(model, parameter, value) but it varies (Kb+Ks)/N instead of only Kb. I have also tried a 'for loop command,' however this runs the simulations one at a time. Please how do I solve this problem?
As suggested in the comments, you can't do what you want with a simple gain block. Replace Kb with a Repeating Sequence block, a From Workspace block, or whatever source signal you want to use. Then add that signal to a constant block Ks/N and multiply the output of the add block to whatever signal was previously going through the gain block.
I have a system with some embedded Matlab blocks where I'd like to perform some actions after a certain amount of time, in this case turn on lights and switches in an interface to which I send signals from Simulink.
The problem is that I thought I'd use "tic"-"toc" and "while" in a Matlab function block to perform these actions, say one parameter becoming 1 after 5 seconds, the following parameter becoming 1 after 12 seconds and so on, but I noticed that tic-toc apparently doesn't work in Simulink for embedded functions.
Is there any similar functions that could be used in Simulink for embedded functions or is there any other way to do this?
Edit: I've tried to get the clock's time as well, but it's a growing value. Is there any way to "lock" the time as a parameter when the block's function is executed?
You shouldn't be using absolute time in an embedded system, which is at least one of the reasons why tic-toc and clock from MATLAB don't work with Simulink Coder.
You should create your own counter, which you start and stop when you need to.
This is pretty easy to do using a Unit Delay and Summation block.
If you need to be able to enable and/or reset the counter then use the appropriate block from the Additional Discrete library.
I want to set a variable step size for every solver step by using the command in the S-function like:
dT= ... % calculate the dT from the inputs of Block and the parameters of S-function
set_param(gcs,'...',num2str(dT));
However, the Matlab does not provide us with a assignable parameter like 'Step' for specifing the solver step size by using the command "set_param()" above. The callable and assignalbe parameters for the solver step size are only 'MaxStep' and 'MinStep'. Therefore, the following two commands are acceptable and executable in Simulink:
set_param(gcs,'MaxStep',num2str(dT1));
set_param(gcs,'MinStep',num2str(dT2));
Thus, I was trying to assign a same value to dT1 and dT2 in order to get the a certain step size, but there was immediately a error report indicating that the max. step and min. step cannot be the same.
So my question is how to specify a step size to the solver in the script of S-function?
The solver settings (used by variable step and fixed-step blocks) are set on initialization and can't be changed using the simulation.
And I assume the fixed-step solver suggestion in the comments won't work for you as you seem to indicate that you want to change the step size during the simulation.
Generically there is no real mechanism for you to have that sort of control over defining (on a step by step basis) the step size that Simulink takes during the simulation.
Nominally that's what the Simulink solver does for you automatically based on the settings during initialization.
You can do it on a block by block basis if all the blocks are S-functions and have a variable step size.
And you could do it by running the simulation over a single time step, saving the SimState, determining the next sample time, running for one time step, saving the SimState, etc., but that would be very inefficient.