How to I cumulatively add data to a matrix in a GUI using a push button?

I want to create a GUI with 2 edit boxes and one button. When I type numbers into the edit boxes (e.g. 1 and 2), and then click the button, I want the code to append these to numbers to a matrix N so that it becomes N = [1 2]. When I type new numbers (e.g. 3 7) and press the button, N should become N = [1 2; 3 7]. How do I do this appending process and how can I save this matrix to be used for other functions in the GUI?
Cheers!

 Respuesta aceptada

KL
KL el 29 de Ag. de 2017
Editada: KL el 29 de Ag. de 2017
A = []; %initialize
then something like the following for your pushbutton callback
function pushbutton_callback(src,event)
s1 = get( handles.edit1, 'String');
s2 = get( handles.edit2, 'String');
n1 = str2double(s1);
n2 = str2double(s2);
A = [A; n1, n2];
end
also make sure that you throw a message if they enter anything other than numbers.

10 comentarios

Ahhh so smart! Thank youuu that's perfect! My only question is about the code
A = []; %initialize
Does this go under the opening function?
Adam
Adam el 29 de Ag. de 2017
Editada: Adam el 29 de Ag. de 2017
You would need to attach it to handles, otherwise it will not be available in the callback.
handles.A = [];
in the OpeningFcn if you are using GUIDE and then access it in the callback similarly. You also need to call the guidata function. A GUIDE callback would generally look more like
function pushbutton_callback( hObject, eventdata, handles )
so:
handles.A = [handles.A; n1, n2];
guidata( hObject, handles )
should work.
If you are not using GUIDE there are other approaches.
Thank you so much!
Why doesn't A save the previous values? Right now, all it does it saving the current values and the previous ones just vanish.. Help?
KL
KL el 29 de Ag. de 2017
Editada: KL el 29 de Ag. de 2017
Probaby you need to write A = []; only in the beginning/initialization script ( just once ), not inside your callback.
I edited my comment a little after you first replied to it, which may help.
I'm not entirely sure if that would work, but something that did was writing
guidata(hObject,Handles)
at the end of the pushbutton function!
I don't know why it works, so if you know please explain :p But I'm glad it works now! Thanks for the help!
Thanks Adam.
handles is just a structure, it gets passed into your callback from GUIDE, but what you have is a local copy. If you make changes to it they get lost when the function goes out of scope, like any other local variable.
guidata( hObject, handles )
embeds your updated handles back into the GUI so that next time a callback is triggered it will provide the updated handles.
Ah that makes sense! Thank you both Adam and KL!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Ag. de 2017

Comentada:

el 29 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by