matlab gui showing axis at the background
1 view (last 30 days)
Show older comments
adnan ali awan
on 6 Jan 2016
Commented: adnan ali awan
on 7 Jan 2016
I am trying to plot some data using timer function on a seperate figure. Somehow I always get stucked by axis showing up at the background of my GUI?
function yeahwhatever_OpeningFcn(hObject, eventdata, handles, varargin)
handles.a = timer('ExecutionMode','fixedRate','Period',5,'TimerFcn',@pushbutton1_Callback);
start(handles.a)
handles.output = hObject;
guidata(hObject, handles);
function varargout = yeahwhatever_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
hold on
f = figure();
H = axes('Parent',f);
conn = database('', '', '', 'org.sqlite.JDBC', 'jdbc:sqlite:C:\Users\Uchiha Madara\Desktop\New folder (2)\PedelecManager\Server\sample.db');
curs = exec(conn,'select table1.XAchse from table1');
curs1 = fetch(curs)
data2=curs1.Data;
plot(H,data2);
drawnow()
0 Comments
Accepted Answer
Geoff Hayes
on 6 Jan 2016
Edited: Geoff Hayes
on 6 Jan 2016
adnan - the hold on call in your pushbutton callback is causing the axes to appear within your GUI. According to hold, this function retains plots in the current axes so that new plots added to the axes do not delete existing plots. I think that because the current figure is your GUI with no axes, then the software just creates the axes within it.
Your pushbutton callback code is being called every five seconds and is creating a new figure every time. Given your inclusion of hold on, I'm guessing that you instead want to have just one figure and update its axes whenever the timer expires. In addition to this, you should not use the pushbutton callback as your timer expiry callback because the signatures are different - a handles structure is not one of the input parameters, hObject is not the handle to the pushbutton control, etc. You should separate these two callbacks. Initialize your timer expiry function callback as
handles.a = timer('ExecutionMode','fixedRate','Period',5,'TimerFcn',{@timerCallback,hObject});
where we pass in hObject, the handle to the GUI, as the third input. Now, create your timer callback as
function timerCallback(hObject,eventdata,hGui)
% get the updated handles object
handles = guidata(hGui);
if ~isfield(handles,'hFig')
% create the figure
handles.hFig = figure();
% create the axes for the figure
handles.hAxes = axes('Parent',handles.hFig);
% update the handles object
guidata(hGui,handles);
% apply the hold
hold(handles.hAxes,'on');
end
conn = database('', '', '', 'org.sqlite.JDBC', 'jdbc:sqlite:C:\Users\Uchiha Madara\Desktop\New folder (2)\PedelecManager\Server\sample.db');
curs = exec(conn,'select table1.XAchse from table1');
curs1 = fetch(curs)
% plot the data
data2=curs1.Data;
plot(handles.hAxes,data2);
Note how we check to see if the figure has already been created (and so is a field of the handles structure). If not, then we create the figure, create the axes for that figure, update the handles structure, and apply the hold. We can then query your database and update the axes with the new data.
Try the above and see what happens!
NOTE that since you are querying the data every five seconds, having the pushbutton do the same may not be necessary. If you have added the button so that the user can update the axes faster than the five second rate, then just take the code that would be common to the timer and pushbutton callbacks and put into a separate function that both functions would then call.
3 Comments
Geoff Hayes
on 6 Jan 2016
adnan - in your OpeningFcn, do you still have the code that initializes the output field of handles as
handles.a = timer('ExecutionMode','fixedRate','Period',5,'TimerFcn',{@timerCallback,hObject});
handles.output = hObject;
guidata(hObject, handles);
start(handles.a)
As for the purpose of the push button callback, I don't see why you need it since the timer to collect and plot the data is started in the OpeningFcn with the
start(handles.a);
from above. If you want the push button to start plotting the data, then it should be responsible for starting the timer as
function pushbutton1_Callback(hObject, eventdata, handles)
start(handles.a);
and then remove this line from the OpeningFcn. As an aside, you may want to rename the a field to something more meaningful to make it clear what is supposed to represent.
More Answers (0)
See Also
Categories
Find more on Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!