Matlab如何使设置的回调返回值? [英] Matlab how to make a set callback return values?

查看:998
本文介绍了Matlab如何使设置的回调返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个小脚本,在其中我可以系统地分析许多Matlab图.使用脚本,我应该能够单击图形中的某些点,然后脚本将存储这些值.我现在知道回调函数具有坐标,但是我希望这些值在主文件中存储它们.但是set函数不能从函数接收值.如何创建另一种构造来避免这种情况? [x,y] = set(f,'ButtonDownFcn',{@ Click_CallBack a});不会造成不幸..

I want to make a little script in which I can systematically ananlyse a lot of matlab plots. With the script I should be able to click on some points in the graph and the script then stores these values. I have now that the callback function has the coordinates, but I want these values in the main file to store them. But the set function can't receive values from a function. How can I create another construction to avoid this? [x,y] = set(f,'ButtonDownFcn',{@Click_CallBack a}); doesn't work unfortunalty..

function process_plot()
  dataset_dia = input('diameter?')
  dataset_length = input('length?')


  h = gcf;
  a = gca;
  f =get(gca,'Children');
  set(h, 'Pointer', 'fullcrosshair');
  set(f,'ButtonDownFcn',{@Click_CallBack a}); 

  save(strcat(dataset_dia, '.mat'), x, y);

end

从绘图中提取坐标的函数:

Function that extracts the coordinates from the plot:

function [x, y]= Click_CallBack(h,e,a)
 point = get(a,'CurrentPoint'); x = point(1);
 y = point(4);
 fprintf(1,'X,Y = %.2f,%.2f\n',x,y);
end

推荐答案

您可以执行以下操作.左键单击以将点存储在用户数据中,然后在完成选择将其写入MAT文件后单击鼠标右键.

You could do something like the following. Left click to store points in user data then right click when finished selecting to write them to a MAT file.

function process_plot()   
f =get(gca,'Children');
set(gcf, 'Pointer', 'fullcrosshair');
set(f,'ButtonDownFcn',{@Click_CallBack gca});

function [x, y]= Click_CallBack(h,e,a)
userData = get(a,'userData'); %Store x,y in axis userData
switch get(ancestor(a,'figure'),'SelectionType')
    case 'normal' %left click       
        point = get(a,'CurrentPoint');
        userData(end+1,:) = [point(1,1) point(1,2)];
        set(a,'userData',userData)
        fprintf(1,'X,Y = %.2f,%.2f\n',point(1,1),point(1,2));
    otherwise %alternate click
        % Reset figure pointer
        set(ancestor(a,'figure'), 'Pointer','arrow');
        %Clear button down fcn to prevent errors later
        set(get(gca,'Children'),'ButtonDownFcn',[]);
        %Wipe out userData 
        set(a,'userData',[]);
        x = userData(:,1);
        y = userData(:,2);
        save('myMatFile', 'x', 'y'); %Save to MAT file ... replace name 
end

当然,如果您没有将轴用户数据用于其他用途.另请注意,在按下按钮期间检索到的当前点实际上将不在您绘制的数据集中.它只是光标在绘制线上的当前位置.如果要在绘制的线中找到实际点,则必须在数据中搜索与检索到的光标位置最近的点.

That is of course if you are not using the axis user data for something else. Also note tha the current point retrieved during the button press will not actually be in your plotted data set. It is just the cursors current position over your drawn lines. If you want actual points in your plotted lines you will have to search within your data for the closest point to the retrieved cursor position.

这篇关于Matlab如何使设置的回调返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆