How to create a multi thread gui
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Adrian Bercovici
am 11 Apr. 2016
Kommentiert: Adrian Bercovici
am 12 Apr. 2016
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);
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 11 Apr. 2016
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);
3 Kommentare
Walter Roberson
am 11 Apr. 2016
You initialize your y to N zeros's and in your loop you shrink y by one element each pass through the loop. An all-zero line could easily be mistaken as not being there.
You read data into x in your loop but you do not store or plot that x.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!