MATLAB: How to create a multi thread gui

guiserial

Hello how can i plot data real time in a gui?
The code below keeps blocking.I want when i press the close_port_btn the reading to stop. Since i have seen there is no function to get the serial status of the port i created that flag. This flag is intended to stop the reading. Somehow it seems the code won't exit that part of code. Do i need to do multithreading? And if so how can i achieve that?
function read_btn_Callback(hObject, eventdata,handles)
% hObject handle to read_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global port;
fopen(port);
handles.flag=1;
N=100;
y=zeros(N,1);
t=linspace(0,N,1);
while (handles.flag~=0)
y(1:end-1)=y(2:end);
x=fscanf(port,'%d')/100
plot(y,'LineWidth',2);
grid on;
axis([0 100 0 50])
end
guidata(hObject,handles);
function close_port_btn_Callback(hObject, eventdata, handles)
% hObject handle to close_port_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global port;
fclose(port);
handles.flag=0;
guidata(hObject,handles);

Best Answer

function read_btn_Callback(hObject, eventdata,handles)
% hObject handle to read_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global port;
fopen(port);
handles.flag=1;
guidata(hObject, handles);
N=100;
y=zeros(N,1);
t=linspace(0,N,1);
while true
drawnow();
handles = guidata(hObject);
if handles.flag == 0
break;
end
y(1:end-1)=y(2:end);
x=fscanf(port,'%d')/100
plot(y,'LineWidth',2);
grid on;
axis([0 100 0 50])
end
fclose(port);
function close_port_btn_Callback(hObject, eventdata, handles)
% hObject handle to close_port_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.flag=0;
guidata(hObject,handles);
Related Question