1

我想制作一个小脚本,我可以在其中系统地分析很多 matlab 图。使用脚本,我应该能够单击图表中的某些点,然后脚本存储这些值。我现在知道回调函数有坐标,但我希望主文件中的这些值存储它们。但是 set 函数不能从函数接收值。我怎样才能创建另一个结构来避免这种情况?[x,y] = set(f,'ButtonDownFcn',{@Click_CallBack a}); 不幸的是不起作用..

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
4

1 回答 1

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

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

于 2012-01-03T14:12:32.737 回答