MATLAB Answers

Error while evaluating TimerFcn for timer 'timer-1' SIZE must be greater than 0.

7 views (last 30 days)
Thanh Son Tran
Thanh Son Tran on 21 Jun 2016
Answered: Naman Gupta on 17 Nov 2020
I have done a project to measure heart rate by BPM for 2 months. I created an interface in computer by GUIDE MATLAB. Actually, I got the source code about getting data UART from another and I re-coded it. However, when MATLAB does not notice errors, but it shows this sentence :
Error while evaluating TimerFcn for timer 'timer-1'
SIZE must be greater than 0.
Can anyone know to solve this problems. Thanks a lot.
This is my code to get data UART :
function btn1_Callback(hObject, eventdata, handles)
% hObject handle to btn1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.btn1,'Enable','off');
set(handles.btn2,'Enable','on');
set(handles.Comport,'Enable','off');
set(handles.Baudrate,'Enable','off');
set(handles.start_togglebutton,'Enable','on');
set(handles.PB_Exit,'Enable','on');
global s;
global t;
portList = get(handles.Comport,'String');
portIndex = get(handles.Comport,'Value');
port = portList(portIndex,:);
set(s,'Port',char(port));
brList = get(handles.Baudrate,'String');
brIndex = get(handles.Baudrate,'Value');
br = char(brList(brIndex,:));
br = str2num(br);
set(s,'Baudrate',br);
% fopen(s);
if strcmp(get(hObject,'String'),'Connect')
assignin('base', 't0', 0); % t?o trong workspace t0=0 đ? chu?n b? làm data m?i
s = serial(get_stringPopup(handles.Comport));
s.InputBufferSize = 20000;
s.Baudrate = br;
s.DataBits = 8;
s.Parity = 'none';
s.StopBit = 1;
s.BytesAvailableFcnCount = 1000;
s.BytesAvailableFcnMode = 'terminator';
try
handles.s = s;
fopen(handles.s);
pause(0.2);
delete(timerfindall);
TMR_PERIOD = 0.5;
t = timer('TimerFcn',@(x,y)getDataUART(s),'Period',TMR_PERIOD);
set(t,'ExecutionMode','fixedRate');
start(t);
catch e
if(strcmp(s.Status,'Open'))
fclose(handles.s);
end
errordlg(e.message); %x? l? ngoài l?, n?u không có ngoài l? x?y ra th? s? th?c hi?n catch
end
else
set(hOject,'String','Connect')
fclose(handles.s);
stop(t);
end
guidata(hObject,handles);
function getDataUART(s)
load handles
persistent nPts;
persistent xTime;
persistent yDataCH1;
global b, global start,
data = fread(s,s.BytesAvailable);
% Data
fNaN=find(isnan(data)==1); % mang chua vi tri phan tu NaN
if (~isempty(fNaN))
for i=1:length(fNaN)%lenLaser %length(fNaN)
data(fNaN(i))=0;
end
end
% lay t0 de ve va lam dieu kien reset
t0 = evalin('base', 't0'); % doc bien tu workspace
% Initial variable
if (t0==0)
nPts = 5000; % number of points to display on stripchart
xTime = ones(1,nPts)*NaN;
yDataCH1 = ones(1,nPts)*NaN;
end
% length
lenFrameData=length(data);
% Creat Data
nDataCH1 = [];
% Control Data
synIndex = 1;
% uint8_t UART_Header[5] = {0xFF,0x00}; // ky tu dac biet
while (synIndex < (lenFrameData - 5))
if ((data(synIndex)==255)&&(data(synIndex+1)==0))
nDataCH1 = [nDataCH1 (data(synIndex+3)*256+data(synIndex+2))]; % IR truoc
synIndex = synIndex + 6; % bo qua khung vua roi
else
synIndex = synIndex + 1; % do dong bo
end
end
% Real Data Bases
lenData = length(nDataCH1);
% Update the plot, initial t0=0 in workspace
% t1=length(TimeSecond);
time = t0:1:t0+lenData-1;
t0 = t0+lenData-1; % update th?i đi?m lúc sau
assignin('base', 't0', t0);
% fix up data to change plot
xTime(1:end-lenData) = xTime(lenData+1:end); % shift old data left
xTime(end-lenData+1:end) = time; % new data goes on right
% channel 1
global HeartCountReal, global timecount
yDataCH1(1:end-lenData) = yDataCH1(lenData+1:end); % shift old data left
yDataCH1(end-lenData+1:end) = 0 + 1*nDataCH1*5/1023;
output=yDataCH1;
output1=filter(b,1,output);
heart_processed1=output1(1,end-49:end).^2;
check1=size(find(heart_processed1>1.56),2);
timecount=timecount+1;
if check1>0
HeartCountReal=HeartCountReal+1;
end
% HeartCountReal
% timecount
if timecount==120;
set(handles.bpm_edit,'string',num2str(HeartCountReal))
if HeartCountReal <60
set(handles.heartStatus_text,'string','LOW');
elseif HeartCountReal<=100
set(handles.heartStatus_text,'string','NORMAL');
else
set(handles.heartStatus_text,'string','HIGH');
end
HeartCountReal=0;
timecount=0;
end
global timelength,global strengthEx,global save_exper, global save_heart,global heart_count,global countdown
if start==1
save_heart=[save_heart output1(1,end-49:end)];
assignin('base', 'save_heart', save_heart)
out1=output1(1,end-49:end).^2;
S=size(find(out1>1.7),2);
if S>0
heart_count=heart_count+1;
end
timelength=timelength+1;
if rem(timelength,2)==0
countdown= countdown-1;
set(handles.countdown_text,'string',[num2str(countdown) ' s'])
end
if timelength==30
start=0;
set(handles.start_togglebutton,'value',0);
HeartRate=heart_count*4;
set(handles.hr_text,'string',[num2str(HeartRate) ' BPM']);
if HeartRate <60
set(handles.hb_text,'string','LOW');
elseif HeartRate<=100
set(handles.hb_text,'string','NORMAL');
else
set(handles.hb_text,'string','HIGH');
end
save_exper=[save_exper strengthEx];
assignin('base', 'save_exper', save_exper)
heart_count=0;
end
end
assignin('base', 'yDataCH1', yDataCH1); % tao trong workspace
assignin('base', 'xTime', xTime); % tao trong workspace
xmax=max(xTime);
% plot in MATLAB
set(handles.axes3,'NextPlot','add'); % lenh hold on trong GUI
grid(handles.axes3, 'on');
% plot(handles.axes1,xTime,yDataCH1,'r-','LineWidth',2) ;
plot(handles.axes3,xTime,output1,'r-','LineWidth',2) ;
title(handles.axes3,'OXY SATURATION OF FINGER 1')
axis(handles.axes3,[xmax-400 xmax 0 7]);
save('handles.mat', 'handles');
function doublePopup = get_doublePopup(hObject,handles)
val = get(hObject,'Value');
string_list = get(hObject,'String');
selected_string = string_list{val}; % convert from cell array
% to string
doublePopup = str2double(selected_string);
% Function to
function stringPopup = get_stringPopup(hObject,handles)
val = get(hObject,'Value');
string_list = get(hObject,'String');
stringPopup = string_list{val}; % convert from cell array

  1 Comment

Jan
Jan on 21 Jun 2016
Please post the complete error message and do not let use guess the line, which causes the error.
load handles loads the file handles.mat from the hard disk. It is very inefficient to do this twice a second.
A hint:
fNaN = find(isnan(data)==1); % mang chua vi tri phan tu NaN
if (~isempty(fNaN))
for i=1:length(fNaN)%lenLaser %length(fNaN)
data(fNaN(i))=0;
end
end
Nicer, faster and simpler:
data(isnan(data)) = 0;

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!