Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How to increment index value of variable on every click of Push button?

Asked by Arun Badigannavar on 4 Mar 2013

I want to set variable A1=1 wen user clicks Push Button1,A2=1 wen user clicks second time the same push button,A3=1 wen user clicks same button for third time,,,like this variable index should increase wenever user clicks the push button ,,




2 Answers

Answer by Jan Simon

Jan Simon (view profile)

on 4 Mar 2013
Accepted answer

Do not do this. See Answers: FAQ How to create variables A1, A2, ... in a loop.

It is much smarter to store the variables as a vector or cell array, such that you can access the value by A{1} or A(1), respectively. Then the button's callback can store the counter in the handles struct:

function buttonCallback(ObjectH, EventData)
handles = guidata(ObjectH);
handles.counter    = handles.counter + 1;
handles.A{counter} = 1;
guidata(ObjectH, handles);

Then the counter and the list of variables ist stored in the FIGURE's ApplicationData an can be obtain by GUIDATA.


Jan Simon

Jan Simon (view profile)

Answer by Ajay Pherwani

Ajay Pherwani (view profile)

on 4 Mar 2013

how many variables you have ? are the numbers of variables fixed or they may vary ?

If varibales are fixed : suppose we have A1.. to ..An

Assuming last push button hit made An=1 and what should happen when one more push button hits ?


Ajay Pherwani

Ajay Pherwani (view profile)

Contact us