フィルターのクリア

Running handles from GUI using timer object

3 ビュー (過去 30 日間)
Lem
Lem 2011 年 10 月 12 日
Hello,
I am a beginner to MATLAB, tried searching on the help docs and Google, but wasn't able to figure out why my timer object won't run a particular GUI handle. I generated the GUI using GUIDE. The problem I have, in brief is following: I can call my function 'rando' from a button callback, but it doesn't seem to work if I call it from a timer object.
If you could please help me with this, I'd appreciate it. Thanks in advance. Here's the code:
function varargout = trial(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @trial_OpeningFcn, ...
'gui_OutputFcn', @trial_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
% --- Executes just before trial is made visible.
function trial_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to trial (see VARARGIN)
% Choose default command line output for trial
handles.output = hObject;
% [X, map] = imread('C:\Users\Lemonickous\Documents\Courses\DC\DCseminar\images, etc\dc\untitled.jpg');
% image(X)
% colormap(map)
% axis off
handles.a = timer;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes trial wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = trial_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%trial2;
delete(handles.figure1);
% --- Executes on button press in generate.
function generate_Callback(hObject, eventdata, handles)
% hObject handle to generate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t2 = randi(10);
set(handles.a, 'ExecutionMode', 'fixedrate');
set(handles.a, 'Period', .1);
set(handles.a, 'TasksToExecute', t2);
set(handles.a, 'TimerFcn', 'rando(hObject, eventdata, handles)');
start(handles.a)
% --- Executes on button press in button2.
function button2_Callback(hObject, eventdata, handles)
% hObject handle to button2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rando(hObject, eventdata, handles);
function rando(hObject, eventdata, handles)
t = randi(3);
if t == 1
out = 200000;
elseif t == 2
out = 240000;
else
out = 280000;
end
set(handles.disp, 'String', num2str(out));

採用された回答

Walter Roberson
Walter Roberson 2011 年 10 月 12 日
Any callback coded as a string is executed within the context of the base workspace. The base workspace does not have a rando function defined in it, as rando is private to the file trial.m
You should instead pass a function handle.
Do it like this:
function generate_Callback(hObject, eventdata, handles)
% hObject handle to generate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t2 = randi(10);
set(handles.a, 'ExecutionMode', 'fixedrate');
set(handles.a, 'Period', .1);
set(handles.a, 'TasksToExecute', t2);
set(handles.a, 'TimerFcn', {@rando, hObject});
start(handles.a)
% --- Executes on button press in button2.
function button2_Callback(hObject, eventdata, handles)
% hObject handle to button2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rando(hObject);
function rando(hObject)
t = randi(3);
if t == 1
out = 200000;
elseif t == 2
out = 240000;
else
out = 280000;
end
handles = guidata(hObject);
set(handles.disp, 'String', num2str(out));
  1 件のコメント
Lem
Lem 2011 年 10 月 12 日
Thanks a lot Walter, it worked!

サインインしてコメントする。

その他の回答 (1 件)

Jeff D.
Jeff D. 2011 年 12 月 7 日
I'm trying to do something similar. This isn't an answer, but a question about your code and GUI. Are pushbutton2, generate, and button2 all different push buttons? What is the rando() function for? I'm trying to use a variable from a different m-file (presently trying it as a global) but don't understand how to tie it to the timer function, i.e. I want to update the GUI periodically without pressing a button.
  7 件のコメント
Jeff D.
Jeff D. 2011 年 12 月 7 日
I was actually doing it the other way round, i.e. calling the GUI from the other routine, but could probably change and call my main file from the gui.
Walter Roberson
Walter Roberson 2011 年 12 月 7 日
The order doesn't really matter. When you call the GUI it will probably create the figures and uicontrols and set up their callbacks -- and then, having done those, return control to the calling routine. The figure number of the GUI is often returned as part of that.
The only important bit about the order is the "obvious in retrospect": the handle you wish to update in the GUI must have been created before you try to update it.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by