2012-01-02 180 views
1

我想制作一个小脚本,我可以系统地分析大量的matlab图。通过脚本,我应该可以点击图表中的某些点,然后脚本将存储这些值。现在我已经知道回调函数具有坐标,但我希望在主文件中存储这些值。但是set函数不能从函数接收值。我怎样才能创建另一个建筑来避免这种情况? [x,y] = set(f,'ButtonDownFcn',{@ Click_CallBack a});不unfortunalty工作..Matlab如何设置回调返回值?

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 [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 
+3

您应该阅读Matlab文档中关于GUI中数据通信主题的一些相关部分,我认为它们可以对您的情况有所帮助:[在编程GUI中管理数据的方式](http:// www.mathworks.fr/help/techdoc/creating_guis/f13-998352.html)和[在GUI的回调中共享数据](http://www.mathworks.fr/help/techdoc/creating_guis/f13-998449.html# f13-1000011) – Aabaz 2012-01-02 16:03:53

回答

0

你可以做类似如下。左键单击以将点存储在用户数据中,然后在完成选择将其写入MAT文件时单击鼠标右键。

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 

这当然,如果你不使用轴用户数据的其他东西。另外请注意,按下按钮期间检索的当前点实际上不会出现在绘图数据集中。这只是游标在您的画线上的当前位置。如果你想在你的绘制线中的实际点,你将不得不在你的数据内搜索最近点到检索到的光标位置。