How to create a counter without using for and while loop.

2 次查看(过去 30 天)
below is a part of the codes we used in our monitor software for our special project using arduino and power analyzer.
my problem is.. i cannot store all the data sent by the arduino. the counter does not function. it also displays single line array. can someone help me with this. I've been debugging it whole day.
opening GUI function
handles.counter = 0;
seconnect_callback
if strcmp(get(hObject,'String'),'CONNECT') % currently disconnected
serPortn = get(handles.sercomm, 'Value'); % serialport is list of serial ports available
if serPortn == 1
errordlg('Select valid COM port');
else
serList = get(handles.sercomm,'String');
serPort = serList{serPortn};
serConn = serial(serPort,'BaudRate',9600,'DataBits',8,'StopBits',1,'FlowControl','none' );
set(serConn, 'ReadAsyncMode', 'continuous');
try
fopen(serConn);
set(handles.serconnect, 'String','DISCONNECT');
set(handles.sercomm, 'Enable','off');
set(serConn, 'BytesAvailableFcn', {@myfunc,handles});
catch e
errordlg(e.message);
end
end
else
set(hObject, 'String','CONNECT');
set(handles.serconnect, 'BackGroundColor','default');
set(handles.sercomm, 'Enable','on');
fclose(serConn);
msgbox('Saving data to Excel....');
below is oart of my function (there are 4 sets of data, below are just two of them)
function myfunc(hObject, eventdata, handles)
global P1 P2 P3 Q1 Q2 Q3 V1 V2 V3 PTnew QTnew VT
global serConn
count = 0;
try
RxText = fscanf(serConn);
RxText = strtrim(RxText);
pause(0.1)
[id,InRange,power,va,var,pf,volt,current]=strread(RxText,'%s%s%s%s%s%s%s%s','delimiter',',');
%%%MAIN POWER ANALYZER
if strcmp(id,'D') %DATA FOR MAIN PA
%count = count + 1;
handles.counter = handles.counter + 1;
%%%%CONVERT STRING TO DOUBLE %%%%%%
PT= str2double(power);
QT=str2double(var);
VT= str2double(volt);
PTnew = PT - 8.7;
QTnew = QT - 99.8;
set(handles.LP4,'String',PTnew);
set(handles.LQ4,'String',QTnew);
set(handles.LV4,'String',volt);
LOAD(handles.counter,:) = [PTnew QTnew VT];
end
if strcmp(id,'A') %DATA FOR LOAD 3
%count = count + 1;
%handles.counter = handles.counter + 1;
set(handles.LP3,'String',power);
set(handles.LQ3,'String',var);
set(handles.LV3,'String',volt);
%%%%CONVERT STRING TO DOUBLE %%%%%%
P3= str2double(power);
Q3=str2double(var);
V3= str2double(volt);
LOAD(handles.counter,:) = [P3 Q3 V3];
end
catch e
disp(e)
end

回答(1 个)

Walter Roberson
Walter Roberson 2013-3-16
set(serConn, 'BytesAvailableFcn', {@myfunc,gcf});
[...]
function myfunc(hObject, eventdata, thisfig)
handles = guidata(thisfig);
[...]
guidata(thisfig, handles); %at end of function

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by