Hi, I'm looking to create a button counter such that each time this particular button is pressed, the counter increases by 1. However, it's not working. My code is below.
I realize that "handles.count==1;" is what's causing the code to keep referring to "if handles.count==1". However, if I remove "handles.count==1", the code does not work at all. Can anyone give me assistance to get the counter to work?
FYI: "fcn" is a function i already created.
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)
handles = guidata(gcbo);
handles.count = 1;
if handles.count == 1
handles.count= handles.count+1;
fcn(1)
handles.count = 2;
elseif handles.count == 2
handles.count= handles.count+1;
fcn(2)
elseif handles.count == 3
handles.count= handles.count+1;
fcn(3)
else
set(hObject, 'enable', 'off')
end

2 件のコメント

hardik sanghvi
hardik sanghvi 2015 年 11 月 26 日
This code is for infinite no to be count or limited count
hardik sanghvi
hardik sanghvi 2015 年 11 月 26 日
For 100 count code will be ? Pls tell

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

回答 (2 件)

Sean de Wolski
Sean de Wolski 2012 年 5 月 22 日

0 投票

Something like this for all it (accept the enable off when it gets to 4).
val = str2double(get(handles.count,'string'));
fcn(val);
set(handles.count,'string',num2str(val+1));
More Since the button being pressed is the one to be augmented:
val = str2double(get(hObject,'string'));
fcn(val);
set(hObject,'string',num2str(val+1));
No Name
No Name 2012 年 5 月 22 日

0 投票

If I place that snippet of code in my code (above my if-else statements), I get these errors:
??? Error using ==> get
Invalid handle object.
Error in ==> GUI>start_Callback at 134
val = str2double(get(handles.count,'string'));
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> GUI at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)GUI('start_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback

1 件のコメント

Sean de Wolski
Sean de Wolski 2012 年 5 月 22 日
Well do you have a button named count? Apparently not. It looks like you named the button 'start'. If you put a break point on the line val =... and push the button:
What are the fields of handles.
Actually you could skip the call to handles altogether if the button being pressed is the one running the callback and just use hObject. See addendum

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

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

質問済み:

2012 年 5 月 22 日

コメント済み:

2015 年 11 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by