Real-Time Interface between App in AppDesigner and Simulink using parsim - Error: not enough input arguments @(x)addlistener
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
More questions to the topic.
Error using nhf>@(x)addlistener(x,'SimulationOutput','PostSimFcn',listener) (line 233)
Not enough input arguments.
in my simulink-modell i do the following. Is something wrong here?
Should i write listener = @nhf (Class)?

This function run in nhf (Class)
function Simulate(obj)
for simNr = numel(obj.Data):-1:1
in(simNr) = Simulink.SimulationInput(obj.simModel);
for prop = fieldnames(obj.Data(simNr))'
in(simNr) = in(simNr).setVariable(char(prop), obj.Data(simNr).(char(prop)));
% tbd: assign to baseworkspace
% assignin('base',char(prop), obj.Data(simNr).(char(prop)));
end
if isdeployed
in(simNr) = simulink.compiler.configureForDeployment(in(simNr));
end
end
% - Disable the automatic opening of the visualization viewer
% - Should be done for all models before compiling
% -------------------------------------------------------------
%open_system(obj.simModel);
%set_param(obj.simModel, 'SimMechanicsOpenEditorOnUpdate', 'off');
% Set up the listener for progress updates
listener = @(src,event) updateProgress(app,event);
% Save results internally
obj.simOut = parsim(in, ...
'ShowProgress','on',...
'TransferBaseWorkspaceVariables','on',...
'SetupFcn',@(x) addlistener(x, 'SimulationOutput', 'PostSimFcn', listener));
What is wrong in the function ('SetupFcn', @(x) addlistener(x, 'SimulationOutput', 'PostSimFcn', listener) ?
Is the function in simulink wrong?
0 Kommentare
Akzeptierte Antwort
Gayathri
am 5 Feb. 2025
Bearbeitet: Gayathri
am 5 Feb. 2025
The error you are encountering is because the arguments given to the "addlistener" function is not correct. The function required an "event" as input and the "postSimFcn" will not be considered as an event here.
Please make sure that "SimulationOutput" is defined as an event in the class definition as follows.
events
Simulationoutput % Define a custom event
end
Also, make sure to add the "setPostSimFcn" as shown below.
for simNr = numel(obj.Data):-1:1
in(simNr) = Simulink.SimulationInput(obj.simModel);
for prop = fieldnames(obj.Data(simNr))'
in(simNr) = in(simNr).setVariable(char(prop), obj.Data(simNr).(char(prop)));
% tbd: assign to baseworkspace
% assignin('base',char(prop), obj.Data(simNr).(char(prop)));
in(simNr) = in(simNr).setPostSimFcn(@(simOut) obj.postSimFunction(simOut, simNr));
end
if isdeployed
in(simNr) = simulink.compiler.configureForDeployment(in(simNr));
end
end
Please modify the "parsim" command as follows to correct the arguments of the "addlistener" function.
obj.simOut = parsim(in, ...
'ShowProgress', 'on', ...
'TransferBaseWorkspaceVariables','on',...
'SetupFcn', @(x) addlistener(obj, 'Simulationoutput', @updateProgress));
Now, you can modify the post simulation function to notify the listener once the event is completed. You can decide on when to call the "notify" function. But below is an example on how you can use the function in your implementation.
function simOut = postSimFunction(obj, simOut, simNr)
try
% Example: Save each simulation output to a file
save(sprintf('output_%03d.mat', simNr), 'simOut');
% Notify listeners that the simulation output is ready
notify(obj, 'Simulationoutput', SimulationCompletedData(simNr, simOut));
catch ME
disp(['Error in PostSimFcn: ', ME.message]);
end
end
classdef SimulationCompletedData < event.EventData
properties
SimulationNumber
SimulationOutput
end
methods
function data = SimulationCompletedData(simNr, simOut)
data.SimulationNumber = simNr;
data.SimulationOutput = simOut;
end
end
end
Here, I have provided the normal workflow and syntaxes, you can modify the code as required for "AppDesigner".
For more information on "addlistener" and "notify" function, please refer to the below documentation links.
Hope you find this information helpful!
9 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Data Preparation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!