Multiple Plot GUI can't be minimized

11 views (last 30 days)
Adrian Bercovici
Adrian Bercovici on 13 Jun 2016
Commented: Walter Roberson on 13 Jun 2016
Hello i am building a gui with multiple Plots (AXES) and it seems i am not able to get it minimized while in execution.It gets coming back. I am reading the data from an Arduino ,i open the port then i press this button and i plot continuosly the arrays y1 and y0 on two different plots. I read the data one row at a time, first i write it in a file then i split it in cells for later data identification.
Any idea on why the i can't minimize it? (It minimizes for a second then comes back)
function read_btn_Callback(hObject, eventdata, handles)
global myport;
handles=guidata(hObject);
y0=zeros(handles.samples,1);
y1=zeros(handles.samples,1);
i=0;
a=fopen('Data.txt','wt');
while(strcmp(handles.port_stt,'opened'))
handles=guidata(hObject);
drawnow
y0(1:end-1)=y0(2:end,1);
y1(1:end-1)=y1(2:end,1);
if(strcmp(handles.port_stt,'opened'));
data_row=fscanf(myport,'%s');
fprintf(a,strcat(data_row,'\n'));
string=strsplit(data_row,',');
guidata(hObject,handles);
y0(end)=str2double(string{1})/100;
y1(end)=str2double(string{4})/100;
y2=str2double(string{2});
end
axes(handles.axes1);
plot(y0,'--','LineWidth',2,'Color','g');
grid on;
ylim([0 100]);
axes(handles.axes2);
plot(y1,'--','LineWidth',2,'Color','g');
grid on;
ylim([0 100]);
pause(0.0001);
end
fclose(a);

Answers (2)

Walter Roberson
Walter Roberson on 13 Jun 2016
plot(handles.axes1, y0,'--','LineWidth',2,'Color','g');
grid(handles.axes1, 'on');
ylim(handles.axes1, [0 100]);
plot(handles.axes2, y1,'--','LineWidth',2,'Color','g');
grid(handles.axes2, 'on');
ylim(handles.axes2, [0 100]);
drawnow(); %instead of pause
Also you can remove the
handles=guidata(hObject);
and the
guidata(hObject,handles);
as you are not changing the handles structure, unless you are doing so in a routine you have not shown here. I do see your tests against handles.port_stt which suggest that you might be doing so (if you do have something going on in the background like that, you need to worry that it is maximizing your figure). If you do have a control that is determining the state of port_stt then it might make sense to fetch the updated guidata, but it would be more efficient to make the state a property of a control and only check the property (even if it is a UserData property): every time you fetch guidata() you are fetching all of the structure. Also, remember if something external is updating the handles structure then it is updating the "master" copy of the structure so there is no need for you to update the master handles structure with the same values -- and if you do update the master structure then you run into the risk of race conditions where the other one has updated guidata but you have not received that update yet so you accidentally overwrite with the old value.

Adrian Bercovici
Adrian Bercovici on 13 Jun 2016
Thank you for the reply.I thought i need to save all the fields in which i'm making changes in handles that is why i use guidata almost in every function. I need the pause since the while will not exit (it's a singlethread from what i assume).
  1 Comment
Walter Roberson
Walter Roberson on 13 Jun 2016
drawnow() and pause() both allow graphic interrupts.
You only need to guidata(hObject, handles) if you have changed a field in handles; you do not need to call it if you have only changed a property of a handle that is stored in handles. I would speculate from your code that whatever is setting handles.port_stt is changing the actual field value: if so then you would need to use guidata(hObject) to fetch the new value, but you could be more efficient by instead setting a property (possibly UserData) of a handle and then just asking for an updated copy of that property.

Sign in to comment.

Categories

Find more on Graphics Performance 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!