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

27 次查看(过去 30 天)
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?

采纳的回答

Jon
Jon 2023-9-14
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 个评论
Felipe Teixeira Roberto
编辑:Felipe Teixeira Roberto 2023-9-14
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 2023-9-15
编辑:Jon 2023-9-15
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.

请先登录,再进行评论。

更多回答(2 个)

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

Guillaume
Guillaume 2023-9-14
编辑:Guillaume 2023-9-14
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 个评论
Felipe Teixeira Roberto
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Event Functions 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by