How to make Simulink read a MatLab variable every 5 seconds?
35 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Let's assume a simple logic in MatLab that, every 5 seconds the variable ' i ' is increased to '+1' in a inf while loop.
Is it possible to use the ' i ' as input in a Simulink paced simulation in a way that the value is constantly been updated while the MatLab code is running?
0 Commenti
Risposta accettata
Jon
il 14 Set 2023
You can do this using the set_param function. I have attached an example Simulink model and script you can run to demonstrate this. For simplicity I set the value using a loop with a pause. This will block you from doing anything else on the command line while it is running. I would suggest that you implement similar logic using a timer if you want to continue to use the command line while the simulation runs. If you don't know how to do this please let me know and I can give you an example.
% Increment value in Simulink using loop with pause
Tupdate = 1; % update period [s]
blockName = 'SetExternalExample/Constant' %constant block whose value is to be changed
% Start infinite loop to update value
% you will have to ctrl-c to break out of this loop
keepRunning = true;
count = 0;
while keepRunning
% Change block value to current count
set_param(blockName,'Value',num2str(count))
pause(Tupdate)
% Increment counter
count = count + 1;
end
I don't think that @Guillaume's approach will work, as Simulink doesn't read from the workspace once the simulation has started running.
4 Commenti
Jon
il 15 Set 2023
Modificato: Jon
il 15 Set 2023
Glad to hear that this approach worked for you.
Regarding how to become more proficient at MATLAB/Simulink, I can suggest the following.
If you haven't completed them already work through all of the exercises in the MATLAB and Simulink On Ramps https://matlabacademy.mathworks.com/details/matlab-onramp/gettingstarted, https://matlabacademy.mathworks.com/details/simulink-onramp/simulink Even if you already know most of the material in those you will still probably pick up some things you didn't know.
Otherwise, whenever you get stuck, try Google searching for what you are trying to do, being sure to include the word matlab in your search terms, so for example you could search something like: matlab find repeated elements in vector This will often point you to an answer in MATLAB Answers, or to the MATLAB documentation, both of which you can also search directly, but I find that often I get to what I'm looking for more quickly by Google searching it
Finally definitely keep making use of MATLAB answers, and post questions as you did here, I have learned a lot from this site and asking questions myself.
Più risposte (2)
Fangjun Jiang
il 14 Set 2023
Use the "MATLAB Function" block to integrate your MATLAB algorithem directly into a Simulink model.
Guillaume
il 14 Set 2023
Modificato: Guillaume
il 14 Set 2023
Hello,
In Simulink you can use a Constant block or any other block that reads a Workspace variable value.
In your Matlab function you could then change the value of this variable using:
assignin('base', 'i', i+1);
Hope this helps.
As pointed by @Jon, this will not work because Simulink will not actualize the variable value during simulation. So you should use the set_param method.
2 Commenti
Vedere anche
Categorie
Scopri di più su Event Functions in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!