creating a timer in GUI

10 次查看(过去 30 天)
Hi I'm using GUI for do a spectrum analyzer interface. My problem is when I create a timer and call my function, appears a warning, something like that:
"the "handles" estructure is no defined or recognized"
thi is a part of my code:
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
obj1 = instrfind('Type', 'gpib', 'BoardIndex', 0, 'PrimaryAddress', 18, 'Tag', '');
if isempty(obj1)
obj1 = gpib('ni', 0, 18);
else
fclose(obj1);
delete(obj1);
clear obj1;
obj1 = gpib('ni', 0, 18);
end
% Configure instrument object, obj1.
set(obj1, 'InputBufferSize', 10000);
set(obj1, 'OutputBufferSize', 10000);
% Connect to instrument object, obj1.
set(obj1,'Timeout',5);
fopen(obj1);
Nombre = query(obj1, '*IDN?');
set(handles.text1,'String',Nombre);
fprintf(obj1,':FORM:BORD SWAP');
fprintf(obj1,':FORM:DATA REAL,32');
% Get the nr of trace points
nr_points = str2double(query(obj1,':SWE:POIN?'));
% Get the reference level
ref_lev = str2num(query(obj1,':DISP:WIND:TRAC:Y:RLEV?'));
% Put the instrument in continuos mode
fprintf(obj1,':INIT:CONT ON');
% Create and bring to front fi gure number 1
%figure(1)
% Create a plot handle, ph, and draw a line at the refl evel
handles.axes1=(1:nr_points);
ph = plot(handles.axes1,ref_lev*ones(1,nr_points));
% Adjust the x limits to the nr of points
% and the y limits for 100 dB of dynamic range
xlim([1 nr_points])
ylim([ref_lev-100 ref_lev])
% Activate the grid
grid on
t =timer('timerfcn',@graficar,'Period',0.1); //THIS IS MY TIMER
handles.varobj1=obj1;
handles.varph=ph;
start(t);
guidata(hObject,handles);
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function graficar(hObject, eventdata, handles) //THIS IS MYFUNCTION
obj1=handles.varobj1; //THE WARNING REFERS TO THIS
ph=handles.varph; //LINE OF CODE
fprintf(obj1,':TRAC? TRACE1');
data = binblockread(obj1,'float32');
fscanf(obj1); %removes the terminator character
% Change the plot line data (fast update method)
set(ph,'Ydata',data);
% fl ushes the plot event queue
drawnow
guidata(hObject,handles);

采纳的回答

Walter Roberson
Walter Roberson 2012-5-17
t = timer('timerfcn', {@graficar, gcf},'Period',0.1);
and
function graficar(hObject, eventdata, fignum)
handles = guidata(fignum);
  3 个评论
Luke Somers
Luke Somers 2018-9-12
Where did 'gcf' come from? Does it even matter what you put there? How do you know that it's going to be a figure or an element of one? Is it a global?
Walter Roberson
Walter Roberson 2018-9-15
Better would probably have been
t = timer('timerfcn', {@graficar, hObject}, 'Period',0.1);
What needs to go in that location is any graphic handle belonging to the same figure that is the GUI.
Based upon the style and variable names, we can tell the framework was generated by GUIDE, and for GUIDE, the first parameter of the function, hObject, is always the same figure as the handles are attached to. In truth, GUIDE has a bug or limitation in this matter, that it can only properly manage the handles structure (third parameter) for callbacks for graphics objects in the same figure as the GUI.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by