MATLAB:从按钮的循环创建的复选框中导出数据

时间:2015-10-23 20:29:24

标签: matlab for-loop checkbox legend qpushbutton

在创建一个带有图形的窗口的代码和一些(ii)复选框加载一些数据(每个复选框一个数据)之后,我想通过按钮导出它们只有勾选了匹配一组数据的复选框

首先,我创建了(在" il_raffa"的帮助下)一些复选框,它需要灵活(ii:输入循环),我的代码可以在其中汇总这样的原则:

GUI窗口

N=500;
M=300;

handles.fig=figure('Units', 'Pixels','Position',[100 100 N M]);

handles.axes=axes('Units', 'Pixels','tag','axes_tag','Position',[25 25 N-200     M-50]);

for ii=1:4; %input exemple

   handles.check{ii}=uicontrol('style','checkbox','string', ...
      ['Display_file_' num2str(ii)],'tag',['c_b_' num2str(ii)], ...
      'position',[N-150 M/2-ii*20 100 25],'callback','plot_sel_cb(handles.axes)');
end

复选框回调

function plot_sel_cb(ax_h)
cb_id=get(gcbo,'string')
cb_pos=get(gcbo,'position')
val=get(gcbo,'value')
path=get(gcbo,'userdata');
axes(ax_h)
hold on

if val

   x=1:10;
   y=[1:10].*cb_pos(2)/100;
   plot(x,y,'color,[rand(1) rand(1) rand(1)])
   legend(cb_id)

    set(h,'tag',cb_id)


else 
    delete(findobj('tag',cb_id))
end

从这段代码中我想创建一个按钮,它只从勾选的复选框中获取[x,y]来创建一个包含所有这些数据的变量。

第二个问题是,我还没有设法创建一个传奇,以及#34;情节(x,y,'颜色,[rand(1)rand(1)rand(1)] )"添加名称" cb_id"选中与数据匹配的复选框时。

帮助

由于

1 个答案:

答案 0 :(得分:1)

让我从第二期开始:添加一个传奇。

checkbox callback中,根据复选框的status(已选中/未选中)添加/删除行。

要在图例中添加项目非常简单,只需将所需的字符串(在您的情况下为cb_id)插入cellarray并将其设置为{{1的输入参数功能。

要在取消选中复选框时从图例中删除项目,而离开另一个则可能会有点困难。

考虑到你似乎没有那么多的线条来绘制,最简单的(即使不是最佳的")方法可以改变legend的工作方式。

Yuo可以对其进行编码,以便每次调用它时:

  • 删除所有先前绘制的行
  • 删除当前图例
  • 根据复选框状态
  • 绘制(循环)所有行
  • 创建合适的图例

在下文中,您可以找到已实现此方法的回调函数的更新版本。

为了交换数据​​(例如复选框的句柄),您可以将它们分配给图形的checkbox callbak属性。

例如,它可以用作结构,因此您可以将数据分配给其字段。

例如,您在main函数中有userdata个复选框句柄;使用以下代码,您可以指定复选框的数量及其句柄:

ii

我已经在下面的所有功能中使用了这个方法。

关于第一个问题:figure_data.n_cb=ii; figure_data.cb_handles=handles.check; set(handles.fig,'userdata',figure_data) 提取(x,y)值。

您可以在主要功能中添加pushbutton(根据复选框)。

pushbutton uicontrol是:

  • 在开始时禁用(在主GUI功能中)
  • 在选中leat one复选框时启用(在复选框回调中)
  • 否则
  • 禁用(在复选框回调中)

有几种获取(x,y)值的方法,其中两个是: - 您可以将它们分配给复选框回调中的变量(在这种情况下,您实际上不需要按钮) - 您可以利用这样一个事实,即在轴上绘制的每条线的数据也存储在图表句柄中的属性pushbuttonxdata中。

ydata您可以通过axes uicontrol属性访问它们:它会重新绘制轴中绘制的每个元素的句柄,因此您可以编写循环来通过这些句柄获取数据。

原则上,由于您的数据可能不具有相同的大小(与checkbox_1关联的绘图可能包含的数据多于checkbox_2的数据),因此您不应使用矩阵来保存所有数据。

最好使用childrencellarray

数组

在下文中,您可以找到添加了struct ha的主要功能的更新版本以及pushbuton

主要GUI功能

pushbutton callback

更新了N=500; M=300; % Added ",'toolbar','figure'" to show the figure toolbar (zoom, rotate, % ...) handles.fig=figure('Units', 'Pixels','Position',[100 100 N M],'toolbar','figure'); handles.axes=axes('Units', 'Pixels','tag','axes_tag','Position',[25 25 N-200 M-50]); for ii=1:4; %input exemple % Changed definition of "handles.check" from "cellarray" to "array" % Added setting of each checkbox "userdata" to the value of loop index (a % way to automatically identify the checkboxes) % % handles.check{ii}=uicontrol('style','checkbox','string', ... handles.check(ii)=uicontrol('style','checkbox','string', ... ['Display_file_' num2str(ii)],'tag',['c_b_' num2str(ii)], ... 'position',[N-150 M/2-ii*20 100 25],'userdata',ii, ... 'callback','plot_sel_cb(handles.axes)'); end % Add the pushbutton (it is disabled at creation), it will be enabled by % the checkbox callback if any checkbox set handles.button=uicontrol('style','pushbutton','string','Export data', ... 'enable','off','position',[N-150 M/2+50 100 25], ... 'callback','export_data'); % % Store info into figure "userdata" % number of checkboxes % status of the checkboxes (all unchecked at the beginning) % pushbuton handle figure_data.n_cb=ii; figure_data.selected_cb=zeros(1,ii); figure_data.cb_handles=handles.check; figure_data.button=handles.button; % set(handles.fig,'userdata',figure_data)

checkbox callback

按钮回拨

function plot_sel_cb(ax_h)
% Get the checkbox id
cb_id=get(gcbo,'userdata');
% Check if current checkbox has been checked (val=1) or unchecked (val=0)
val=get(gcbo,'value');
% Get the number of checkbox in the GUI
figure_data=get(gcf,'userdata');
n_cb=figure_data.n_cb;
cb_handles=figure_data.cb_handles;

% path=get(gcbo,'userdata');
axes(ax_h);

% Clear axes (delete plot) and delete the legend
p_h=get(gca,'children');
if(~isempty(p_h))
   delete(p_h);
   delete(figure_data.leg_h)
end

hold on

% Update current checkbox status in the checkbox info
figure_data.selected_cb(cb_id)=val;
% Initialize legend string, counter and plot handles array
legend_str={};
cnt=0;
p_h=[];
% Loop throught the checkbox to plot
for i=1:n_cb
   if(figure_data.selected_cb(i))
      cnt=cnt+1;
      x=1:10;
      % Get checkbox position (to generate exmple data to plot)
      cb_pos=get(cb_handles(i),'position');
      % Get checkbox id
      curr_cb=get(cb_handles(i),'userdata');
      y=[1:10].*cb_pos(2)/100;
      % Plot and store plot handle
      p_h(cnt)=plot(x,y,'color',[rand(1) rand(1) rand(1)],'linewidth',2);
      % Set the plot tag to the index of the checkbox (it will be used in
      % the pushbutton callback as index of the cellarray in which to store
      % the data plottd
      set(p_h(cnt),'userdata',curr_cb)
      % Get the string of the current chceckbox
      cb_string=get(cb_handles(i),'string');
      % Replace "_" with "_-" for the correct visualization
      cb_string=strrep(cb_string,'_','_-');
      % Build the legend's string
      legend_str{cnt}=cb_string;
   end
end
% Add the updated legend and store its handle in figure_data
if(~isempty(legend_str))
   figure_data.leg_h=legend(p_h,legend_str);
end
% Manage "Export data" pushbutton enabling
if(any(figure_data.selected_cb))
   set(figure_data.button,'enable','on')
else
   set(figure_data.button,'enable','off')
end
% Update figure data
set(gcf,'userdata',figure_data);

enter image description here

希望这有帮助。