Error while evaluating TimerFcn for timer 'timer-1' Reference to non-existent field 'fileID'. How to pass fileID to timer? Thanks My code:

67 views (last 30 days)
function varargout = untitled1(varargin)
% % % % %
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @keithley_OpeningFcn, ...
'gui_OutputFcn', @keithley_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
function keithley_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
delete(instrfindall);
set(findobj('Type','uicontrol'), 'BusyAction','cancel', 'Interruptible','off');
%------------------------------------------------------------------------------%
g = instrfind('Type', 'gpib', 'BoardIndex', 0, 'PrimaryAddress', 10, 'Tag', '');
if isempty(g)
g = gpib('ni', 0, 10);
else
fclose(g);
delete(g);
clear obj1;
g = gpib('ni', 0, 10);
end
g.EOSMode = 'read&write';
g.Timeout = 1;
handles.g = g;
%------------------------------------------------------------------------------%
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Initial period is 1 sec.
'TimerFcn', {@readdata,handles}); % Specify callback function
% Update handles structure
guidata(hObject, handles);
function varargout = keithley_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function measure_button_Callback(hObject, eventdata, handles)
fopen(handles.g);
fprintf(handles.g,':OUTP ON'); % output on
fprintf(handles.g,':INIT'); % start
fileID = fopen(handles.fullfile,'a'); %handle to file
handles.fileID = fileID;
readdata(hObject,eventdata,handles);
start(handles.timer)
guidata(hObject,handles)
function readdata(hObject,eventdata,handles)
fprintf(handles.g,':READ?'); % trigger and aquire reading
read = fscanf(handles.g);
date_string = datestr(now,'dd-mmm-yyyy HH:MM:SS.FFF');
fprintf(handles.fileID,'%s;%s',date_string,read);
guidata(hObject,handles)
function save_button_Callback(hObject, eventdata, handles)
[filename, pathname] = uiputfile({'*.txt'},'Save as');
if isequal(filename,0) || isequal(pathname,0)
disp('User selected Cancel')
else
disp(['User selected ',fullfile(pathname,filename)])
handles.fullfile = fullfile(pathname,filename);
handles.filename = filename;
end
guidata(hObject,handles)
function Exit_pushbutton_Callback(hObject, eventdata, handles)
delete(handles.timer)
close all;
function stop_measurements_button_Callback(hObject, eventdata, handles)
stop(handles.timer);
fclose(handles.fileID);
fprintf(handles.g,':OUTP OFF'); % output off
fclose(handles.g);
guidata(hObject,handles);

Answers (3)

Steven Lord
Steven Lord on 2 Feb 2016
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Initial period is 1 sec.
'TimerFcn', {@readdata,handles}); % Specify callback function
This probably won't do what you want. This will pass a copy of the handles structure as it exists when this line of code is executed into your readdata function. Any changes to the handles structure in this function, or to the "master copy" of the handles structure (like the fileID field you add to the handles structure inside measure_button_Callback), will NOT be recognized.
In order to allow the readdata function to access the most up-to-date handles structure, instead of passing in the handles structure pass in something that's not going to change but from which you can retrieve the handles structure, like the handle to the main GUI figure (which you can use with GUIDATA inside the TimerFcn.)
  7 Comments
Lukasz Wozniak
Lukasz Wozniak on 8 Feb 2016
Dear Walter Roberson I did as You suggested and now I have an error: "H must be the handle to a figure or figure descendent."
Steven Lord
Steven Lord on 15 Feb 2016
Don't use GCBF here unless you're creating the timer inside one of the callback functions of a component in your GUI. Get the handle to the GUI you want to access in your timer and use that handle, whatever it's called, to set your TimerFcn.
function figureColorChangeExample
qwerty = figure;
% qwerty contains the figure handle so I use it to construct t
t = timer('ExecutionMode', 'fixedRate', ...
'Period', 1, ...
'TimerFcn', {@changeColor,qwerty});
start(t)
pause(10)
stop(t)
close(qwerty)
function changeColor(hObject,eventdata,handleToFigure)
set(handleToFigure, 'Color', rand(1, 3));
When you execute this function it should bring up a figure, change its color for ten seconds, then close the figure.

Sign in to comment.


Lukasz Wozniak
Lukasz Wozniak on 8 Feb 2016
Edited: Lukasz Wozniak on 8 Feb 2016
For clarity I am pasting the whole code, with Your suggestions. And the whole error messege when I tried to press measure button:
"Error using guidata (line 89) H must be the handle to a figure or figure descendent.
Error in untitled1>readdata (line 71) handles = guidata(fig);
Error in untitled1>measure_button_Callback (line 63) readdata(hObject,eventdata,handles);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in untitled1 (line 17) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)untitled1('measure_button_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback"
function varargout = untitled1(varargin)
% % % % %
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @keithley_OpeningFcn, ...
'gui_OutputFcn', @keithley_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
function keithley_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
delete(instrfindall);
set(findobj('Type','uicontrol'), 'BusyAction','cancel', 'Interruptible','off');
%----------------------------------------------------------------------------%
g = instrfind('Type', 'gpib', 'BoardIndex', 0, 'PrimaryAddress', 10, 'Tag', '');
if isempty(g)
g = gpib('ni', 0, 10);
else
fclose(g);
delete(g);
clear obj1;
g = gpib('ni', 0, 10);
end
g.EOSMode = 'read&write';
g.Timeout = 1;
handles.g = g;
gcbf = gcf;
%------------------------------------------------------------------------------%
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Initial period is 1 sec.
'TimerFcn', {@readdata,gcbf}); % Specify callback function
% Update handles structure
guidata(hObject, handles);
function varargout = keithley_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function measure_button_Callback(hObject, eventdata, handles)
fopen(handles.g);
fprintf(handles.g,':OUTP ON'); %
fprintf(handles.g,':INIT'); % start measurements
fileID = fopen(handles.fullfile,'a');
handles.fileID = fileID;
readdata(hObject,eventdata,handles);
start(handles.timer)
guidata(hObject,handles)
function readdata(hObject,eventdata,fig)
handles = guidata(fig);
fprintf(handles.g,':READ?'); % trigger and aquire reading
read = fscanf(handles.g);
date_string = datestr(now,'dd-mmm-yyyy HH:MM:SS.FFF');
fprintf(handles.fileID,'%s;%s',date_string,read);
guidata(hObject,handles)
function save_button_Callback(hObject, eventdata, handles)
[filename, pathname] = uiputfile({'*.txt'},'Save as');
if isequal(filename,0) || isequal(pathname,0)
disp('User selected Cancel')
else
disp(['User selected ',fullfile(pathname,filename)])
handles.fullfile = fullfile(pathname,filename);
handles.filename = filename;
end
guidata(hObject,handles)
function Exit_pushbutton_Callback(hObject, eventdata, handles)
delete(handles.timer)
close all;
function stop_measurements_button_Callback(hObject, eventdata, handles)
stop(handles.timer);
fclose(handles.fileID);
fprintf(handles.g,':OUTP OFF'); % output off
fclose(handles.g);
guidata(hObject,handles);

Lukasz Wozniak
Lukasz Wozniak on 15 Feb 2016
So, my problem is unsolvable?

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!