matlab指南:将数据从循环中存储到向量中(matlab GUIDE: storing data from loop in vector)

编程入门 行业动态 更新时间:2024-10-22 19:42:59
matlab指南:将数据从循环中存储到向量中(matlab GUIDE: storing data from loop in vector)

我是matlab的新手,我需要一些帮助。 我正在尝试制作一些程序,你可以通过2次点击图片获得一些长度的东西。 基本上它运作良好,但在尝试对它进行算法时遇到了问题。 我使用ginput获取坐标,然后使用毕达哥拉斯定理与它们一起工作并制作重要的变量z2 。 那些我想要存储在矢量中的变量如此基本:我在图片上按了两次push_button1并获得了一些z2,现在当我再次执行该程序时,我想将旧的一个存储在同一个矢量中[z2_old z2_new] 。 但基本上每当我试图制作这样的东西时,旧的值就会被重写。

这是我的按钮的代码:

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部分(工作权)

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);

%创建z2变量(工作正确)

x2=c2(1,1)-c2(2,1); y2=c2(1,2)-c2(2,2); z2=sqrt(x2*x2+y2*y2);

% - 获取向量的循环(不行)

for i=1:counter z(i,1)=z2 end

所以,基本上当我第一次运行按钮并且z2得到例如130,第二次60,第三次210m我想得到z = [130 60 210],现在我仍然得到像第一次按下的东西:z = [130 ],第二次按:z = [60 60],第三次按:z = [210 210 210],旧值每次丢失。 我试图修复它很多方面,但它仍然无法正常工作。 它只是我工作的一小部分,我几乎没有时间。

有人可以告诉我这个循环应该怎么样? 我试过z(end+1)=z2 , z=[z z2] ,但结果总是一样的。

非常感谢你的帮助。 对此,我真的非常感激。

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.

最满意答案

问题是每按一次按钮,它就会为z2创建新值。 即使计数器递增,但z2始终是新值。 因此,您需要在按钮的点击之间保存z2 。 为此,您可以在GUI回调之间存储可用于数据存储的handles结构。 你需要做两个改变 -

在guidata(hObject, handles);更新句柄结构之前,在guiname__OpeningFcn添加这一行guidata(hObject, handles); -

handles.z2 =[];

编辑按钮回调到此 -

x2=c2(1,1)-c2(2,1) y2=c2(1,2)-c2(2,2) z2=sqrt(x2*x2+y2*y2) z = [handles.z2 z2] handles.z2 = z; guidata(hObject,handles);

希望这对您有用,请告诉我们!

The issue is that with each click on pushbutton, it creates new values for z2. Even though the counter increments, but z2 is always the new value. So, you need to save z2 between clicks on the pushbutton. For that you have handles structure available for data storage between GUI callbacks. You need to make two changes -

Add this line inside the guiname__OpeningFcn before the handles structure is updated in guidata(hObject, handles); -

handles.z2 =[];

Edit the pushbutton callback to this -

x2=c2(1,1)-c2(2,1) y2=c2(1,2)-c2(2,2) z2=sqrt(x2*x2+y2*y2) z = [handles.z2 z2] handles.z2 = z; guidata(hObject,handles);

Hope this works out for you, let us know!

更多推荐

本文发布于:2023-07-07 21:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068526.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   指南   数据   matlab   GUIDE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!