MATLAB: Problem with plotting using a GUI and Simulink

guiguideMATLABplottingsimulinktimer

I am trying to Plot realtime position data On to a GUI axes. My first attempt was using a listner function but i could only plot the data if i used the method of
s=plot(x_data,y_data) set(s,'Parent',axes2)
This would result in the data being plotted but i could not discover a way of keeping my axis fixed at the point [-1,1,-1,1] for the x and y limits.The plot would shift to the amount of change in the data.
To try and overcome this i started using timer functions which would activate at a period of 0.01 seconds that would call the data from simulink.
Again i came up with the same problem. with also a further problem is that the plot would not automatically update. I had to use a push button to force update it. This makes little sense to me as the function on the timer should carry on doing this.
Can anyone help with any of my problems ? My code is below.
% — Executes on button press in plot_data. function plot_data_Callback(hObject, eventdata, handles) % hObject handle to plot_data (see GCBO) % eventdata reserved – to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) axis_plotter=timer; set(axis_plotter,'executionmode','fixedRate') set(axis_plotter,'Period',0.01) set(axis_plotter,'TimerFcn',@plot_axis) start(axis_plotter)
function plot_axis(source,event)
global new_xdata
global new_ydata
rto = get_param('manualflight/2dplot','RuntimeObject');
ydata = rto.InputPort(1).Data ;
xdata = rto.InputPort(2).Data ;
new_xdata=[xdata,new_xdata];
new_ydata=[ydata,new_ydata];
set(axes2.handles, 'XLim', [-1,1], 'YLim', [-1,1])
set(axes2.handles,'XData',new_xData,'YData',new_yData);
I also tried
--- Executes on button press in plot_data.
function plot_data_Callback(hObject, eventdata, handles)
% hObject handle to plot_data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axis_plotter=timer;
set(axis_plotter,'executionmode','fixedRate')
set(axis_plotter,'Period',0.01)
set(axis_plotter,'TimerFcn',@plot_axis)
start(axis_plotter)
function plot_axis(source,event)
global new_xdata
global new_ydata
rto = get_param('manualflight/2dplot','RuntimeObject');
data = rto.InputPort(1).Data ;
xdata = rto.InputPort(2).Data ;
new_xdata=[xdata,new_xdata];
new_ydata=[ydata,new_ydata];
b=plot(new_xdata,new_ydata);
set(b,'Parent',axes2)
drawnow update
This code came up with the problem of not updating and having a too small of an axis

Best Answer

You need to register Simulink execution event listeners for the plot to update as the simulation runs. See Listening for Method Execution Events and How can I update a GUI with values from my Simulink model as it is running?