当另一个按钮处于活动状态时,使用计数器更新按钮字符串

时间:2015-06-23 01:33:55

标签: matlab matlab-guide

我在打开功能中设置句柄:

exec php-cli -c /path/to/your/php.ini "$CONSOLE"/cake.php "$@"

然后,当按下标记为“下一步”的按钮时,我试图将句柄更改为pushbutton1:

function Select_A_B_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;
handles.string = '';
new_count = 1;
set(handles.counter,'String',num2str(new_count));
if isempty(varargin)
    varargin{1} = 1;
    varargin{2} = 1;
end
A = {'Apple';'Orange';'Bag';'Cowboy'};
handles.pushbutton1text = A;

new_count = str2double(handles.counter.String);
handles.pushbutton1 = handles.pushbutton1text(new_count);

guidata(hObject, handles);

当我尝试将句柄设置为pushbutton1时出现以下错误: function next_Callback(hObject, eventdata, handles) current_count = str2double(get(handles.counter, 'String')); new_count = current_count+1; set(handles.counter,'String',new_count); set(handles.pushbutton1,'String',get(handles.counter,'string'); guidata(hObject, handles);

我已经尝试了几种方法来修复错误,但还没有成功。我做错了什么?

1 个答案:

答案 0 :(得分:0)

这是一个程序化的GUI,它可以满足您的需求,在我看来,它更易于理解/调试。您可以使用GUIDE轻松实现此功能;按钮回调之前的所有内容都可以放入GUI Opening_Fcn

我在代码中添加了评论;如果有什么不清楚请告诉我。

function DisplayFruits

clear
clc

hFig = figure('Position',[200 200 300 300]);

handles.A = {'Apple';'Orange';'Bag';'Cowboy'};
handles.counter = 1;

%// Counter text
handles.CounterTitle = uicontrol('Style','text','Position',[50 200 60 20],'String','Counter');

handles.CounterBox = uicontrol('Style','text','Position',[130 200 60 20],'String','1');

%// Content of A
handles.TextTitle = uicontrol('Style','text','Position',[50 170 60 20],'String','Content');

handles.TextBox = uicontrol('Style','text','Position',[130 170 60 20],'String',handles.A{handles.counter});

%// Pushbutton to increment counter/content
handles.PushButton = uicontrol('Style','push','Position',[130 100 80 40],'String','Update counter','Callback',@(s,e) UpdateCallback);

guidata(hFig,handles);

%// Pushbutton callback
    function UpdateCallback      

        %// Update counter
        handles.counter = handles.counter + 1;

        %// If maximum value possible, set back to 1.
        if handles.counter == numel(handles.A)+1;
            handles.counter = 1;
        end

        %// Update text boxes
        set(handles.CounterBox,'String',num2str(handles.counter));

        set(handles.TextBox,'String',handles.A{handles.counter});

        guidata(hFig,handles);
    end
end

GUI的示例屏幕截图:

enter image description here

当用户按下按钮时,计数器会递增,“内容框”会更新。

希望这有帮助!

作为旁注,我认为你上面得到的错误是由于这个命令:

handles.pushbutton1 = handles.pushbutton1text(new_count);

这里你为按钮指定了一个字符串,但后来你尝试修改它的String属性,Matlab不喜欢它。