What is causing error " Error evaluating 'StartFcn' callback of block_diagram 'MotorModel'. Undefined function or variable 'localAddE​ventListen​er'."

22 views (last 30 days)
i started trying to work with GUI and simmulink. I created a simulink model of a motor and modified a piece of code found online to give me the following code:
function callsim
%define the model name
ModelName = 'MotorModel';
%open the simulink model
load_system(ModelName);
% Simulink may optimise your model by integrating all your blocks. To
% prevent this, you need to disable the Block Reduction in the Optimisation
% settings.
set_param(ModelName,'BlockReduction','off');
%call the eventlistner
set_param(ModelName,'StartFcn','localAddEventListener');
% set the stop time to inf
set_param(ModelName,'StopTime','inf');
% set the simulation mode to normal
set_param(ModelName,'SimulationMode','normal');
% Set a listener on the Gain block in the model's StartFcn
set_param(ModelName,'StartFcn','localAddEventListener');
% start the model
set_param(ModelName,'SimulationCommand','start');
%create a line to plot coordinates
global ph;
ph = line(0,0);
% When simulation starts, Simulink will call this function in order to
% register the event listener to the block 'SineWave'. The function
% localEventListener will execute everytime after the block 'SineWave' has
% returned its output.
eventhandle;
function eventhandle = localAddEventListener
eventhandle = add_exec_event_listener('MotorModel/Motor', ...
'PostOutputs', @localEventListener);
end
% The function to be called when event is registered.
function localEventListener(block, eventdata)
disp('Event has occured!')
%global ph;
% Gets the time and output value
simTime = block.CurrentTime;
simData = block.OutputPort(1).Data;
% Gets handles to the point coordinates
xData = get(ph,'XData');
yData = get(ph,'YData');
% Displaying only the latest n-points
n = 200;
if length(xData) <= n
xData = [xData simTime];
yData = [yData simData];
else
xData = [xData(2:end) simTime];
yData = [yData(2:end) simData];
end
% Update point coordinates
set(ph,...
'XData',xData,...
'YData',yData);
% The axes limits need to change as you scroll
samplingtime = .01;
%Sampling time of block 'SineWave'
offset = samplingtime*n;
xLim = [max(0,simTime-offset) max(offset,simTime)];
set(gca,'Xlim',xLim);
drawnow;
end
end
i am not too sure why i get the error and was hoping i could have someone help me understand where i am going wrong here

Answers (1)

Walter Roberson
Walter Roberson on 6 Oct 2015
The line
set_param(ModelName,'StartFcn','localAddEventListener');
needs to be changed to
set_param(ModelName,'StartFcn',@localAddEventListener);
However, notice the line that currently reads
eventhandle;
right before the line
function eventhandle = localAddEventListener
That line right before "function" will almost certainly need to be commented out:
%eventhandle;
  7 Comments
Walter Roberson
Walter Roberson on 6 Oct 2015
The simplest possibility would be that MotorModel/Motor does not exist. Try putting a try/catch around the add_exec_event_listener and displaying the content of the exception structure.

Sign in to comment.

Categories

Find more on Simulink in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!