1
votes

I'm building a GUI that shows images when I push a button, but there are a lot of images, and sometimes I want to jump over a number of images, so what I'm trying to do is, get a edit texbox, where I put the number of the image I want to go, push a button to push the other button until i get to the desired image. I want to make this way( pushing the other button x times to get to the desired image) because there are a bunch of parallel stuff happening at the same time the image is passing to the other.

so my code for the button is:

function pushbutton14_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
i = str2double(get(handles.edit4, 'String'));
while handles.counterN < i
    pushbutton1_Callback(hObject, eventdata, handles);
    guidata(hObject, handles);
end

So every time I push button 14, I want to get it to push the button 1, until the counter(which is added 1n every time button 1 is pushed) gets to the value in edit4.

But for some reason the guidata is not updated, so the counter always stays as 1.

I have guidata(hObject, handles); , in the end of the button 1 function... so I don't understand why is not updated, I also tried without guidata(hObject, handles); in the while loop

1
I just updated my answer. Forgot that you need the counter at the beginning of the loop. - Mad Physicist

1 Answers

2
votes

pushbutton1_Callback is incrementing handles.counterN, but you never get the incremented value. In fact, you are immediately overwriting it with 1 when that callback returns. Remember that within the scope of pushbutton14_Callback, handles.counterN is 1. pushbutton1_Callback may be incrementing the counter, but not within the scope of the calling function. Setting it with guidata will not update the value in pushbutton14_Callback until you call handles = guidata(hObject);. Since pushbutton1_Callback has already called guidata(hObject, handles);, get instead of setting the data:

Replace guidata(hObject, handles); with handles = guidata(hObject);.