MATLAB: How to use four listeners and plot them on four different axes in a GUI

axeslisteners

Hi, I'm using four listeners in my GUI and I'm trying to plot them on four different axes. However, they are always plotted on the same axes. I created four axes, but the data is always plotted on the last axes (axes currenty). I've tried to pass the handles to the function, but it hasn't worked. Below, I'm sending my program. I hope someone can help me with this problem.
function eixoY_Callback(hObject, eventdata, handles)
global valor_eixoY;
valor_eixoY = roundn(get(handles.eixoY,'Value'),-1);
set(handles.string_eixoY,'String',num2str(valor_eixoY));
if (valor_eixoY > 2.5 && valor_eixoY <= 5)
axisY = ((valor_eixoY - 2.5)*(24-0))/((5-2.5)+0);
set_param('Model_CMH_R9/VaL','Value',num2str(axisY));
guidata(hObject,handles);
end
if (valor_eixoY == 2.5)
axisY = 0;
set_param('Model_CMH_R9/VaL','Value',num2str(axisY));
guidata(hObject,handles);
end
if (valor_eixoY >= 0 && valor_eixoY < 2.5)
axisY = ((valor_eixoY - 2.5)*(-24-0))/((0-2.5)+0);
set_param('Model_CMH_R9/VaL','Value',num2str(axisY));
guidata(hObject,handles);
end
%-------------------------------------------------------------------------------------------------------------------------------------------------





function pushbutton_StartStop_Callback(hObject, eventdata, handles)
global ModelName ph
mystring = get(hObject,'String');
status = get_param(ModelName,'SimulationStatus');
if strcmp(mystring,'Start Simulation')
if strcmp(status,'stopped')
set_param(ModelName,'StopTime','inf');
set_param(ModelName,'SimulationMode','normal');
set_param(ModelName,'BlockReduction','off');
set_param(ModelName,'StartFcn','localAddEventListener');
set_param(ModelName,'SimulationCommand','start')
end
set(handles.pushbutton_StartStop,'String','Stop Simulation')
% cla(handles.axes1)
% cla(handles.axes2)
% Create a line handle
ph(1) = line(0,0);
ph(2) = line(0,0);
ph(3) = line(0,0);
ph(4) = line(0,0);
elseif strcmp(mystring,'Stop Simulation')
if strcmp(status,'running')
set_param(ModelName, 'SimulationCommand', 'stop')
end
set(handles.pushbutton_StartStop,'String','Start Simulation')
else
warning('Unrecognized string for pushbutton_startstop')
end
assignin('base','GUI_Joystick_handles',handles)
assignin('base','StartStop_hObject',handles.pushbutton_StartStop)
%-------------------------------------------------------------------------------------------------------------------------------------------------
function eventhandle = localAddEventListener
eventhandle(1) = add_exec_event_listener('Model_CMH_R9/MotorR', ...
'PostOutputs', @localEventListener1);
eventhandle(2) = add_exec_event_listener('Model_CMH_R9/MotorL', ...
'PostOutputs', @localEventListener2);
eventhandle(3) = add_exec_event_listener('Model_CMH_R9/Veloc', ...
'PostOutputs', @localEventListener3);
eventhandle(4) = add_exec_event_listener('Model_CMH_R9/Rot', ...
'PostOutputs', @localEventListener4);
%-------------------------------------------------------------------------------------------------------------------------------------------------
function localEventListener1(block, eventdata)
% hFigure = GUI_Joystick();



% handles = guidata(hFigure);



global ph;
simTime = block.CurrentTime;
simData = block.OutputPort(1).Data;
xData = get(ph(1),'XData');
yData = get(ph(1),'YData');
n = 100000;
if length(xData) < n
newXData = [xData simTime];
newYData = [yData simData];
else
newXData = [xData(2:end) simTime];
newYData = [yData(2:end) simData];
end
set(ph(1),'XData',newXData,'YData',newYData);
% plot(handles.axes1,newXData,newYData);
%-------------------------------------------------------------------------------------------------------------------------------------------------
function localEventListener2(block, eventdata)
% hFigure = GUI_Joystick();
% handles = guidata(hFigure);
global ph
simTime = block.CurrentTime;
simData = block.OutputPort(1).Data;
xData = get(ph(2),'XData');
yData = get(ph(2),'YData');
n = 100000;
if length(xData) < n
newXData = [xData simTime];
newYData = [yData simData];
else
newXData = [xData(2:end) simTime];
newYData = [yData(2:end) simData];
end
set(ph(2),'XData',newXData,'YData',newYData);
% plot(handles.axes2,newXData,newYData);
%-------------------------------------------------------------------------------------------------------------------------------------------------
function localEventListener3(block, eventdata)
% hFigure = GUI_Joystick();
% handles = guidata(hFigure);
global ph;
simTime = block.CurrentTime;
simData = block.OutputPort(1).Data;
xData = get(ph(3),'XData');
yData = get(ph(3),'YData');
n = 100000;
if length(xData) < n
newXData = [xData simTime];
newYData = [yData simData];
else
newXData = [xData(2:end) simTime];
newYData = [yData(2:end) simData];
end
set(ph(3),'XData',newXData,'YData',newYData);
% plot(handles.axes3,newXData,newYData);
%-------------------------------------------------------------------------------------------------------------------------------------------------
function localEventListener4(block, eventdata)
% hFigure = GUI_Joystick();
% handles = guidata(hFigure);
global ph;
simTime = block.CurrentTime;
simData = block.OutputPort(1).Data;
xData = get(ph(4),'XData');
yData = get(ph(4),'YData');
n = 100000;
if length(xData) < n
newXData = [xData simTime];
newYData = [yData simData];
else
newXData = [xData(2:end) simTime];
newYData = [yData(2:end) simData];
end
set(ph(4),'XData',newXData,'YData',newYData);
% plot(handles.axes4,newXData,newYData);

Best Answer

I can't see in your code where you created the 4 axis. In any case, all your event listeners do is updating the points of the 4 lines created in your StartStop callback. Since these 4 lines are created in the current axis, the behaviour you see makes sense.
The solution is to create each line in the respective axis. At a guess,
% Create a line handle
ph(1) = line(0, 0, 'Parent', handles.axes1);
ph(2) = line(0, 0, 'Parent', handles.axes2);
ph(3) = line(0, 0, 'Parent', handles.axes3);
ph(4) = line(0, 0, 'Parent', handles.axes4);