Update pushbutton string using counter when another pushbutton is active

159 views Asked by At

I am setting the handles in the opening function:

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);

Then I am trying to change the handles to pushbutton1 when the pushbutton tagged as 'Next' is pushed:

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);

I get the following error when I try to set the handles to pushbutton1: Error using set Conversion to double from cell is not possible.

I have tried several ways to fix the error, but no success yet. What am I doing wrong?

1

There are 1 answers

2
Benoit_11 On BEST ANSWER

Here is a programmatic GUI which does what you want and is, in my opinion, much simpler to understand/debug. You can easily implement this using GUIDE; everything that comes before the pushbutton's callback can be put into the GUI Opening_Fcn.

I added comments in the code; if something is unclear please tell me.

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

Sample screenshot of the GUI:

enter image description here

As the user presses the button, the counter is incremented and the "content box" is updated.

Hope this helps!

As a side note, I think the error you got above was due to this command:

handles.pushbutton1 = handles.pushbutton1text(new_count);

here you assigned a string to the pushbutton but later you try to modify its String property and Matlab does not like it.