How to make Simulink read a MatLab variable every 5 seconds?

24 visualizaciones (últimos 30 días)
Felipe Teixeira Roberto
Felipe Teixeira Roberto el 14 de Sept. de 2023
Editada: Jon el 15 de Sept. de 2023
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?

Respuesta aceptada

Jon
Jon el 14 de Sept. de 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 comentarios
Felipe Teixeira Roberto
Felipe Teixeira Roberto el 14 de Sept. de 2023
Editada: Felipe Teixeira Roberto el 14 de Sept. de 2023
If i may, how have you guys learned so much about MatLab, do you guys have any tips about how to lear it more efficiently?
Jon
Jon el 15 de Sept. de 2023
Editada: Jon el 15 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (2)

Fangjun Jiang
Fangjun Jiang el 14 de Sept. de 2023
Use the "MATLAB Function" block to integrate your MATLAB algorithem directly into a Simulink model.

Guillaume
Guillaume el 14 de Sept. de 2023
Editada: Guillaume el 14 de Sept. de 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 comentarios
Felipe Teixeira Roberto
Felipe Teixeira Roberto el 14 de Sept. de 2023
Guillaume, i would like to thank you for your answer, i am very pleased to know the MatLab community has so many smart and good people.
Guillaume
Guillaume el 15 de Sept. de 2023
You are very welcome Felipe :-) Welcome in this nice community !

Iniciar sesión para comentar.

Categorías

Más información sobre Event Functions en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by