Effacer les filtres
Effacer les filtres

MATLAB GUI, how can I prevent a push button from re-initializing my variables?

1 vue (au cours des 30 derniers jours)
Mohammad Ayoub
Mohammad Ayoub le 17 Déc 2017
Commenté : Mohammad Ayoub le 17 Déc 2017
I am creating a MATLAB GUI and one of the push buttons does a certain set of commands I previously defined. The commands begin with getting two values of the GUI itself (from edit texts) then initialize two matrices according to these values. To be more clear, here is my example:
p = str2double(get(handles.P,'string'));
elev=zeros(l,p);
The problem is whenever I press the push button again, elev is set to zeros again, but my goal is to modify elev every time the push button is pressed - in my example:
elev(j+1,i)= D; %j and i are for loop arguments, and D is obtained from edit text.
Is there anyway possible to prevent the push button from re-initializing my matrix to zeros? I have seen answers including "handles" but I am not that familiar with GUIDE and I am new to it, I would really appreciate a precise explanation. Thank you in advance.
Regards,
M. Ayoub

Réponses (1)

Walter Roberson
Walter Roberson le 17 Déc 2017
If you have a handles structure passed in then you can test
if ~isfield(handles, 'elev')
p = str2double(get(handles.P,'string'));
elev=zeros(l,p);
else
elev = handles.elev;
... do the for loop changing elev
end
handles.elev = elev;
guidata(hObject, handles);
  4 commentaires
Jan
Jan le 17 Déc 2017
Or in other words:
You can store a struct inside the figure, e.g. by using guidata to store it in the figure's ApplicationData property. If you are working with GUIDE, this struct is called "handles", although it can contain everything you want, not just handles of the GUI elements. The general procedure is easy:
% Get the current value of the handles struct from the figure:
handles = guidata(hObject);
When a callback is called, the 3rd input "handles" contains the current value already.
% Modify the struct:
handles.YourData = rand(1,3); % Or whatever you want
% Store the struct in the figure afterwards:
guidata(hObject, handles);
Without the last line, the modifications are lost, when the current callback is finished. "hObject" can be the object, which have triggered the current callback, because guidata() uses its parent figure automatically.
In this way guidata() is a easy way to store values in a figure to share them between callbacks.
Mohammad Ayoub
Mohammad Ayoub le 17 Déc 2017
I know you guys are pointing right at the answer but I am trying to do what you said and I'm not getting any results, I guess I don't quite understand it.
Can you show me a simple example? take this example and show me what code would be written in this situation:
I have a GUI with 2 edit texts, a push button, and a static text (output)
The main goal of this GUI is to add, edit1+edit2 = (change static text to result), push button adds the two results and sets them in the static text.
For this simple program, each time I press the push button, I want the result to be stored in an array (A) corresponding to how many times I press the button.
For example, 3+5 -> button pressed -> static text changes to 8 -> A(i) = 8, where i is how many times I press the button
So the next time I enter 4+2 so A will be equal to [8 6]
Third time 3+1 so A will be equal = [8 6 4]
How can I do that in the GUI config? I am sorry but I need this for a university project and I never took any MATLAB courses so it makes it hard to understand GUI alone...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by