Hi Jose,
I understand that you are trying to exchange data between GUI and Simulink. For creating GUI, I am using App Designer in which I have added a button, Numeric Field and UI Axes.
In the Simulink side I have created a simple model using Sine Wave and a gain block. To fetch data from Simulink, enable Signal Logging for the output Signal using Property Inspector.
In the App Designer, I am using ‘set_param’ to set the value of ‘Gain’ block in Simulink.
function GainValueEditFieldValueChanged(app, event)
gainValue = app.GainValueEditField.Value;
set_param('simpleModel/Gain', 'Gain', string(gainValue));
end
To start capturing the output from Simulink and plot it in real time I am using the 'ButtonPushedFcn' Callback.
function StartStopButtonPushed(app, event)
if app.simRunning
% Stop simulation
stop(app.simTimer);
app.simRunning = false;
app.StartStopButton.Text = 'Start Capture';
else
% Start simulation
app.simRunning = true;
app.StartStopButton.Text = 'Stop Capture';
set_param('simpleModel/Gain', 'Gain', string(app.GainValueEditField.Value));
app.plotHandle = plot(app.UIAxes, nan,nan);
% Start a timer to update the plot
app.simTimer = timer('ExecutionMode', 'fixedRate','Period', 0.5, 'TimerFcn', @(~,~) app.updatePlot());
start(app.simTimer);
end
end
The timer calls the 'updatePlot' function periodically which updates the plot after fetching data from Simulink.
function updatePlot(app)
runID = Simulink.sdi.getCurrentSimulationRun('simpleModel');
runObj = Simulink.sdi.getRun(runID.id);
signalID = runObj.getSignalIDByIndex(1);
signalObj = Simulink.sdi.getSignal(signalID);
set(app.plotHandle, 'XData', signalObj.Values.Time, 'YData', signalObj.Values.Data);
if length(signalObj.Values.Time) > 1
xlim([max(signalObj.Values.Time) - 100, max(signalObj.Values.Time)]); % 86400 seconds in a day
end
end
Please go through the following documentation pages to learn more about ‘set_param’, ‘get_param’ and ‘timer’:
- https://www.mathworks.com/help/releases/R2019b/simulink/slref/set_param.html
- https://www.mathworks.com/help/releases/R2019b/simulink/slref/get_param.html
- https://www.mathworks.com/help/releases/R2019b/matlab/ref/timer-class.html
I hope this helps!
Thanks