user3644943
user3644943

Reputation: 7

MATLAB resets the values of variables

I'm new to MATLAB and I created a program where I'm trying a similar situation to this

Pushbutton1

a = 1
b = 1
c = 1

if (level==1) then
newsize=<some calculations here>
a = newsize

elseif (level==2)
newsize=<some calculations here>
b = newsize

else
newsize=<some calculations here>
c = newsize

end

plot(a,b,c)

But when the 'level' changes, it has to update the 'newsize' on a/b/c . But each time i click the button, the previous variables are being reset. I understand that is logically correct for the program to reset the values, but I cant figure a way to basically "save" the values. I don't if I'm too tired to see this or its more complicated than this, so i would appreciate if you helped me on that!

Thank you!

Upvotes: 0

Views: 51

Answers (2)

Amro
Amro

Reputation: 124563

Did you use the GUIDE tool to interactively build the interface, or did you do it programmatically?

If you are using GUIDE, you could use the handles structure passed to each callback function along with the guidata function to store/retrieve the values.

If you building a GUI programmatically, you could use nested functions to create a closure over variables in the parent function's scope.

Of course there is also global variables, persistent variables, and data stored in GUI components (inside the UserData property, or with getappdata/setappdata functions)

There is a section in the documentation about sharing data between callbacks if you want to read about the various methods.

Upvotes: 0

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33864

If you want to save the past values you can just do this:

a =  [a newsize];

That way when you go through you will add all of the values to an ever increasing list instead of replacing them.

Upvotes: 1

Related Questions