Can't execute this function!!!
Show older comments
Hi guys. I'm trying to call a script (printq.m) to synchronize printed data on a txt file but I get the error:
??? Error while evaluating TimerFcn for timer 'timer-1'
Undefined variable "printq" or class "printq.m".
These are my functions:
Main:
t1 = timer; set(t1,'executionMode','fixedRate'); set(t1,'Period', 0.016); set(t1,'TimerFcn','printq.m'); start(t1) clf p560.plot(q)
sub:
fid = fopen('c:\\angjun.txt','wt'); % Note the 'wt' for writing in text mode fprintf(fid,'%f',q); % The format string is applied to each element of a fclose(fid);
Could you guys help me out?
Thank you
Answers (1)
Image Analyst
on 2 Apr 2014
See this snippet where I create a timer where the timer callback function needs to get the handles structure (from GUIDE):
% Create a new timer object.
% The callback function that will be executed when the timer fires off
% will be called TimerCallBack() and it will take the GUIDE handles
% structure as an input argument
fprintf('Creating timer object...\n');
timerObject = timer('TimerFcn', {@TimerCallBack, handles}, 'ExecutionMode', 'fixedSpacing', 'Period', 0.05);
% This is the callback function that gets executed whenever the timer fires off.
% There are two internal, reserved input arguments hObject and eventdata
% but we don't care about those so replace them with tildes.
% handles is the GUIDE handles structure so we can do things like
% interact with controls, set text labels, display images,
% or whatever we want to do with the user interface.
function TimerCallBack(~, ~, handles)
try
% Some code
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
See this snippet where I just call a function that doesn't require any arguments:
% --- Executes on button press in btnStartTimer.
function btnStartTimer_Callback(hObject, eventdata, handles)
% hObject handle to btnStartTimer (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.objTimer = timer('TimerFcn', @TimerCallback, 'Period', 2.0);
start(handles.objTimer);
% Update handles structure
guidata(hObject, handles);
return;
function TimerCallback()
msgboxw(datestr);
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!