MATLAB: What function should I use that will replace text on push button with next array value each time button is pushed

callbackguipush button

I have created a single column cell array (b), and in my GUI, when I push the push button, I would like for the string on my push button to display the next value in the array with each button press. I feel like there should be a way to do this, but I've tried a while loop, and this just gives the terminal string. This is my code so far:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% read the excel sheet
[num,text,raw] = xlsread('Book1.xlsx','Sheet1','A2:B8','basic');
%change order to Q,A,Q,A...
a = reshape(text',2,7)
b = a(:)
%change text of pushbutton1
i = 0
if i < length(b)
i = i + 1
flash_text = b(i)
set(handles.pushbutton1,'String',flash_text)
end

Best Answer

Make the variable persistent so that it will retain its value from one call to the next.
persistent index;
if isempty(index)
index = 1;
else
index = min(index + 1, length(b));
end
flash_text = b(index);
Related Question