Reputation: 1
I'm new to MATLAB and I've got a question to ask. I am currently doing up this UI which includes a code which is shown below.
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject handle to Start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global s brightness a t;
t = 0.1
a = 1;
readasync(s);
while (a == 1)
brightness = str2double(fscanf(s, '%s', 8));
set(handles.brightness, 'String', num2str(brightness));
disp(brightness);
if brightness < 87
if brightness < 44
fprintf(s, '%s',1);
else
fprintf(s,'%s',2);
end
else
if brightness < 130
fprintf(s, '%s',3);
else
fprintf(s, '%s',4);
end
end
if(a==0)
break;
end
pause(1/10);
end
As you can see, I am currently using a pause function to delay the while loop. However my mentor in college suggested that I use tic toc instead of pause to delay the loop. I do not know how should I go about with it. He has given me this function but I do not know how to implement it. Any advice?
function delay(t)
tic;
while toc < t
end
end
Upvotes: 0
Views: 389
Reputation: 24127
Where you have pause(1/10)
, just write delay(1/10)
instead. They are not quite the same thing, as pause
has a side effect of flushing any graphics updates and GUI callbacks that may have queued up.
Upvotes: 1
Reputation: 607
As excaza indicated you can replace the line pause(1/10);
by delay(1/10);
but the function delay
must be visible to where it is being called (so for example you can create a file in the same directory called delay.m
that contains the code from your mentor).
It might be helpful to point out that replacing the code pause(1/10);
by tic; while toc < 1/10 end;
accomplishes the same thing functionally, even though this isn't as good from a coding practice/readability perspective.
Upvotes: 0