matlab GUI: How to store data from push button, to vector (not loosing old values when re-pressing button)

3 views (last 30 days)
I am new to matlab and i need some help. I am trying to make some program, where you can by 2 clicks on picture get a lenght of something. Basicly it works well, but got a problem when trying to algorytmize it. I use ginput to get coordinates and then pythagorean theorem to work with them and make important variable z2. And those variables I want to store in vector so basicly: I pressed push_button1 click twice on a picture and get some z2,now when i do the procedure again i want to store the new one with the old one in the same vector [z2_old z2_new]. But basicly every time i tried to make something like this the old ones value is rewritten.
This is the code of my push button:
function pushbutton1_Callback(hObject, EventData, handles)
%counter - number of press (working right)
counter = get(hObject, 'UserData') + 1;
set(hObject, 'UserData', counter);
set(handles.text1, 'String', sprintf('%d', counter));
% - ginput part (working right)
c2 = ginput(2);
q3 = text(c2(1,1),c2(1,2),'X', ...
'HorizontalAlignment','center', ...
'Color', [1 1 0], ...
'FontSize',10);
q4 = text(c2(2,1),c2(2,2),'X', ...
'HorizontalAlignment','center', ...
'Color', [1 1 0], ...
'FontSize',10);
%creating the z2 variable(working right)
x2=c2(1,1)-c2(2,1);
y2=c2(1,2)-c2(2,2);
z2=sqrt(x2*x2+y2*y2);
% - loop for getting a vector (not ok)
for i=1:counter
z(i,1)=z2
end
So, basicly when I run the button first time and z2 got for example 130, second time 60, third time 210m I want to get z=[130 60 210], now I am still getting something like 1st press:z=[130], 2nd press: z=[60 60], 3rd press: z=[210 210 210], the old values are every time lost. I tried to fix it many ways but its still not working. Its only a small part of my work and i almost run out of time.
Can someone please tell me how should that loop look like? I tried z(end+1)=z2 , z=[z z2] , but the result is always the same.
Thanks for your help a lot. I really Appreciate it.

Answers (1)

Dishant Arora
Dishant Arora on 15 Mar 2014
Edited: Dishant Arora on 15 Mar 2014
function gui_OpeningFcn(hObject, eventdata , handles)
handles.Z = 0;
function pushbutton1_callback(hObject , eventdata, handles)
% Your Code
z2=sqrt(x2*x2+y2*y2);
handles.Z = [handles.Z , z2]
guidata(hObject , handles);

Community Treasure Hunt

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

Start Hunting!