Unable to attach add_exec_event_listener to a block when using Simulink Compiler
I am trying to get real-time data from simulink model (rtl-sdr receiver) and display it on gui, so I am using `add_exec_event_listener` in Simulink model callback as written here: https://www.mathworks.com/matlabcentral/answers/446302-how-do-i-update-a-gui-designed-in-app-designer-with-data-from-a-running-simulink-model
Interaction with model based on Simulink Compiler functions, e.g. creation a SimulationInput object and simulink.compiler.configureForDeployment.
The problem is that I get an error when the line below is executed:
out=sim(in)
Full text of an error:
Error evaluating ‘StartFcn’ callback of block_diagram ‘gsmAdGui2’.
Callback string is ‘blk = ‘gsmAdGui2/Gain’;
event = ‘PostOutputs’;
listener = @(app, event) updateGUI(hApp);
h = add_exec_event_listener(blk, event, listener);
Caused by:
Error using Simulink.Simulation.internal.DesktopSimHelper
Adding a listener for a block method execution event for ‘gsmAdGui2/Gain’ is not supported. Listeners can be added only when block diagram is executing.
I don’t understand what should I do to correctly attach the listener to a gain block (because it cannot be attached directly to a rtl-sdr). Thank for any help.
Here is main callback function:
function StartButtonPushed(app, event)
app.StartButton.Enable = ‘off’;
app.StopButton.Enable = ‘on’;
app.SamplingRate=app.SampRateEditField.Value;
%Create a SimulationInput object for this model
in=Simulink.SimulationInput(app.modelName);
in=in.setModelParameter(‘StartTime’,’0.01′,’StopTime’, num2str(app.StopTimeEditField.Value));
in = in.setVariable(‘sdrGain’, app.GainEditField.Value);
in = in.setVariable(‘fc’, app.CenterFreqEditField.Value);
in = in.setVariable(‘sampRate’, app.SamplingRate);
in.applyToModel;
in=simulink.compiler.configureForDeployment(in);
out=sim(in);%запуск симуляции
app.StartButton.Enable = ‘on’;
app.StopButton.Enable = ‘off’;
end
Here is updateGUI function that receives real-time data from model:
function updateGUI(app, varargin) %plots fft of received 2048 samples per frame iq stream
% Create an object that gets the run-time value of the specified block
rto = get_param([bdroot,’/Gain’],’RuntimeObject’);
% Update the GUI accordingly
currentTime = rto.CurrentTime;
app.SimTime.Text = num2str(currentTime);
sdrData = rto.InputPort(1).Data;
sdrData=sdrData.*app.W; % hamming window
sdrFft = fftshift(fft(sdrData));
sdrFft=20*log10((abs(sdrFft))/2048);
plot(app.UIAxes, app.freqvec, sdrFft,’b’);
end
Here is model InitFcn: hApp = sdrAdGui8 %name of an app
Here is model StartFcn:
blk = ‘gsmAdGui2/Gain’; %block to listen
event = ‘PostOutputs’;
listener = @(app, event) updateGUI(hApp);
h = add_exec_event_listener(blk, event, listener);I am trying to get real-time data from simulink model (rtl-sdr receiver) and display it on gui, so I am using `add_exec_event_listener` in Simulink model callback as written here: https://www.mathworks.com/matlabcentral/answers/446302-how-do-i-update-a-gui-designed-in-app-designer-with-data-from-a-running-simulink-model
Interaction with model based on Simulink Compiler functions, e.g. creation a SimulationInput object and simulink.compiler.configureForDeployment.
The problem is that I get an error when the line below is executed:
out=sim(in)
Full text of an error:
Error evaluating ‘StartFcn’ callback of block_diagram ‘gsmAdGui2’.
Callback string is ‘blk = ‘gsmAdGui2/Gain’;
event = ‘PostOutputs’;
listener = @(app, event) updateGUI(hApp);
h = add_exec_event_listener(blk, event, listener);
Caused by:
Error using Simulink.Simulation.internal.DesktopSimHelper
Adding a listener for a block method execution event for ‘gsmAdGui2/Gain’ is not supported. Listeners can be added only when block diagram is executing.
I don’t understand what should I do to correctly attach the listener to a gain block (because it cannot be attached directly to a rtl-sdr). Thank for any help.
Here is main callback function:
function StartButtonPushed(app, event)
app.StartButton.Enable = ‘off’;
app.StopButton.Enable = ‘on’;
app.SamplingRate=app.SampRateEditField.Value;
%Create a SimulationInput object for this model
in=Simulink.SimulationInput(app.modelName);
in=in.setModelParameter(‘StartTime’,’0.01′,’StopTime’, num2str(app.StopTimeEditField.Value));
in = in.setVariable(‘sdrGain’, app.GainEditField.Value);
in = in.setVariable(‘fc’, app.CenterFreqEditField.Value);
in = in.setVariable(‘sampRate’, app.SamplingRate);
in.applyToModel;
in=simulink.compiler.configureForDeployment(in);
out=sim(in);%запуск симуляции
app.StartButton.Enable = ‘on’;
app.StopButton.Enable = ‘off’;
end
Here is updateGUI function that receives real-time data from model:
function updateGUI(app, varargin) %plots fft of received 2048 samples per frame iq stream
% Create an object that gets the run-time value of the specified block
rto = get_param([bdroot,’/Gain’],’RuntimeObject’);
% Update the GUI accordingly
currentTime = rto.CurrentTime;
app.SimTime.Text = num2str(currentTime);
sdrData = rto.InputPort(1).Data;
sdrData=sdrData.*app.W; % hamming window
sdrFft = fftshift(fft(sdrData));
sdrFft=20*log10((abs(sdrFft))/2048);
plot(app.UIAxes, app.freqvec, sdrFft,’b’);
end
Here is model InitFcn: hApp = sdrAdGui8 %name of an app
Here is model StartFcn:
blk = ‘gsmAdGui2/Gain’; %block to listen
event = ‘PostOutputs’;
listener = @(app, event) updateGUI(hApp);
h = add_exec_event_listener(blk, event, listener); I am trying to get real-time data from simulink model (rtl-sdr receiver) and display it on gui, so I am using `add_exec_event_listener` in Simulink model callback as written here: https://www.mathworks.com/matlabcentral/answers/446302-how-do-i-update-a-gui-designed-in-app-designer-with-data-from-a-running-simulink-model
Interaction with model based on Simulink Compiler functions, e.g. creation a SimulationInput object and simulink.compiler.configureForDeployment.
The problem is that I get an error when the line below is executed:
out=sim(in)
Full text of an error:
Error evaluating ‘StartFcn’ callback of block_diagram ‘gsmAdGui2’.
Callback string is ‘blk = ‘gsmAdGui2/Gain’;
event = ‘PostOutputs’;
listener = @(app, event) updateGUI(hApp);
h = add_exec_event_listener(blk, event, listener);
Caused by:
Error using Simulink.Simulation.internal.DesktopSimHelper
Adding a listener for a block method execution event for ‘gsmAdGui2/Gain’ is not supported. Listeners can be added only when block diagram is executing.
I don’t understand what should I do to correctly attach the listener to a gain block (because it cannot be attached directly to a rtl-sdr). Thank for any help.
Here is main callback function:
function StartButtonPushed(app, event)
app.StartButton.Enable = ‘off’;
app.StopButton.Enable = ‘on’;
app.SamplingRate=app.SampRateEditField.Value;
%Create a SimulationInput object for this model
in=Simulink.SimulationInput(app.modelName);
in=in.setModelParameter(‘StartTime’,’0.01′,’StopTime’, num2str(app.StopTimeEditField.Value));
in = in.setVariable(‘sdrGain’, app.GainEditField.Value);
in = in.setVariable(‘fc’, app.CenterFreqEditField.Value);
in = in.setVariable(‘sampRate’, app.SamplingRate);
in.applyToModel;
in=simulink.compiler.configureForDeployment(in);
out=sim(in);%запуск симуляции
app.StartButton.Enable = ‘on’;
app.StopButton.Enable = ‘off’;
end
Here is updateGUI function that receives real-time data from model:
function updateGUI(app, varargin) %plots fft of received 2048 samples per frame iq stream
% Create an object that gets the run-time value of the specified block
rto = get_param([bdroot,’/Gain’],’RuntimeObject’);
% Update the GUI accordingly
currentTime = rto.CurrentTime;
app.SimTime.Text = num2str(currentTime);
sdrData = rto.InputPort(1).Data;
sdrData=sdrData.*app.W; % hamming window
sdrFft = fftshift(fft(sdrData));
sdrFft=20*log10((abs(sdrFft))/2048);
plot(app.UIAxes, app.freqvec, sdrFft,’b’);
end
Here is model InitFcn: hApp = sdrAdGui8 %name of an app
Here is model StartFcn:
blk = ‘gsmAdGui2/Gain’; %block to listen
event = ‘PostOutputs’;
listener = @(app, event) updateGUI(hApp);
h = add_exec_event_listener(blk, event, listener); app designer, gui, event listener, simulink compiler MATLAB Answers — New Questions