How can I plot the current input to a system in Simulink?
3 次查看(过去 30 天)
显示 更早的评论
Hi there,
I'm looking for a way to display the current value from a Simulink model in a contour-plot. (So the scopes etc won't help here) It has to be in Simulink as I want it live, and the model shall be exported later.
- XX,YY and ZZ are matrices of dim 60x60
- contourf(XX,YY,ZZ); works fine
- Simulink model that has time dependant inports from external source
- I wanna display the current inport to my Simulink system in that contour plot.
But, please, HOW can that be done??
(If important: It doesn't necessarily have to be in real time. Big step sizes or delays are acceptable)
Thanks
2 个评论
Nandini
2023-7-20
To display the current value from a Simulink model in a contour plot, you can use the MATLAB Function block in Simulink. Here's an example of how you can achieve this:
1. Open your Simulink model.
2. Add a MATLAB Function block to your model. You can find it under the "User-Defined Functions" category in the Simulink Library Browser.
3. Double-click the MATLAB Function block to open the block dialog.
4. In the block dialog, write the following code to update the contour plot with the current value of the input:
function y = fcn(u)
persistent XX YY ZZ
if isempty(XX)
[XX, YY] = meshgrid(1:60, 1:60); % Define the grid for XX and YY
ZZ = zeros(size(XX)); % Initialize ZZ with zeros
end
% Update ZZ with the current value of the input
ZZ(u(1), u(2)) = u(3);
% Plot the contour plot
contourf(XX, YY, ZZ);
% Return the input value
y = u;
end
5. Connect the input signal to the MATLAB Function block. The input signal should be a vector `[x, y, z]`, where `x` and `y` represent the coordinates of the point in the contour plot, and `z` represents the value of the point.
6. Connect the output of the MATLAB Function block back to the model if needed.
7. Set the simulation parameters in Simulink to run the model. You can adjust the step size and simulation duration based on your requirements.
When you run the simulation, the contour plot will be updated with the current value of the input at each time step. The input value will also be passed through the MATLAB Function block if needed.
Note that the contour plot will be updated at each simulation step, so you can control the frequency of updates by adjusting the step size in the simulation parameters.
采纳的回答
Nandini
2023-7-20
To display the current value from a Simulink model in a contour plot, you can use the MATLAB Function block in Simulink. Here's an example of how you can achieve this:
1. Open your Simulink model.
2. Add a MATLAB Function block to your model. You can find it under the "User-Defined Functions" category in the Simulink Library Browser.
3. Double-click the MATLAB Function block to open the block dialog.
4. In the block dialog, write the following code to update the contour plot with the current value of the input:
function y = fcn(u)
persistent XX YY ZZ
if isempty(XX)
[XX, YY] = meshgrid(1:60, 1:60); % Define the grid for XX and YY
ZZ = zeros(size(XX)); % Initialize ZZ with zeros
end
% Update ZZ with the current value of the input
ZZ(u(1), u(2)) = u(3);
% Plot the contour plot
contourf(XX, YY, ZZ);
% Return the input value
y = u;
end
5. Connect the input signal to the MATLAB Function block. The input signal should be a vector `[x, y, z]`, where `x` and `y` represent the coordinates of the point in the contour plot, and `z` represents the value of the point.
6. Connect the output of the MATLAB Function block back to the model if needed.
7. Set the simulation parameters in Simulink to run the model. You can adjust the step size and simulation duration based on your requirements.
When you run the simulation, the contour plot will be updated with the current value of the input at each time step. The input value will also be passed through the MATLAB Function block if needed.
Note that the contour plot will be updated at each simulation step, so you can control the frequency of updates by adjusting the step size in the simulation parameters.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!