2015-10-23 46 views
0

在創建一個帶有圖形和一些(ii)加載某些數據的複選框(每個複選框一個數據)的代碼之後,我想導出它們只有在勾選與一組數據匹配的複選框時才能通過按鈕。MATLAB:從一個循環創建的複選框從按鈕導出數據

首先,我創建了(在「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]編輯複選框來創建一個包含所有這些數據的變量。

第二個問題是,我沒有設法創建一個圖例爲「plot(x,y,'color,[rand(1)rand(1)rand(1)])」,其中添加了名稱「當勾選與數據匹配的複選框時,將顯示「cb_id」。

HELP

感謝

回答

1

讓我從你的第二個問題開始:添加一個傳奇。

checkbox callback中,根據複選框的status(選中/未選中)添加/刪除行。

要添加圖例中的項目是很容易的,你只需要插入所需字符串(在你的情況下cb_id)爲cellarray,並設置爲輸入參數legend功能。

如果在取消複選框的同時取出複選框而將另一個項目從圖例中刪除,則可能會有點困難。

考慮到你似乎沒有太多的線條來繪製,最簡單的(即使不是「最佳」)方法是改變checkbox callbak的工作方式。

宥可以編碼它,以便在每次調用一次,它:

  • 刪除所有先前繪製的線
  • 刪除當前圖例
  • 圖(在循環中)中的所有根據線該複選框狀態
  • 建立在以下適當的傳說

,你可以找到的,一個更新版本回調uin這個方法已經被執行了。

爲了交換數據​​(例如複選框的手柄),您可以將它們分配給圖的userdata屬性。

例如,它可能被用作結構體,因此您可以將數據分配給其字段。

作爲一個例子,你已經在主要功能ii複選框句柄;用下面的代碼,你可以指定複選框的數量和它們的句柄:

figure_data.n_cb=ii; 
figure_data.cb_handles=handles.check; 
set(handles.fig,'userdata',figure_data) 

我在所有你可以在下面找到函數使用該aprroach。

關於第一個問題:提取(x,y)值的pushbutton

您可以在主功能中添加pushbutton uicontrol(根據複選框)。

pushbutton是:

  • 在開始禁止(在主GUI功能)
  • 當在真皮休閒一個複選框被選中(在複選框中的回調)
  • 否則禁用(在複選框中啓用回調)

有幾種方式來獲取(X,Y)值,二則是: - 你可以將它們分配給一個變量中的複選框回調(在這種情況下,您實際上不需要按鈕) - 您可以利用這樣一個事實,即繪製在軸上的每條線的數據也存儲在圖的句柄中的屬性xdataydata中。

axes uicontrol可以通過children屬性訪問它們:它會重新繪製軸中繪製的每個元素的手柄,因此您可以編寫一個循環來處理這些手柄並獲取數據。

原則上,由於您的數據可能尺寸不一樣(與checkbox_1關聯的圖可能具有比checkbox_2更多的數據),因此不應使用矩陣來保存所有這些數據。

這將是更好地使用cellarraystruct

數組在下面,你可以在其中找到添加了pushbuton公頃的主要功能和pushbutton callback的更新版本。

主GUI功能

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

按鈕回調

function export_data 
% Get plotted data and set store them into a variable 
% the vaaible can be either a "cellarray" or an array of "struct" 
% The data of each line plotted are stored in the "xdata" and "ydata" 
% property of each "children" of the axis 
p_h=get(gca,'children'); 
n_plot=length(p_h); 
% Loop through the children to retreive the data 
for i=1:n_plot 
    cb_id=get(p_h(i),'userdata'); 
    x_data=get(p_h(i),'xdata'); 
    y_data=get(p_h(i),'ydata'); 
    % Create the cellarray "exported_data_cell_array" 
    exported_data_cell_array{cb_id}=[x_data;y_data]; 
    % Create the struct "exported_data_struct" 
    exported_data_struct(cb_id).checkbox_id=cb_id; 
    exported_data_struct(cb_id).x_data=y_data; 
    exported_data_struct(cb_id).y_data=y_data; 
end 
% Store the extractged data into th e figure_data 
figure_data=get(gcf,'userdata'); 
figure_data.exported_data_cell_array=exported_data_cell_array; 
figure_data.exported_data_struct=exported_data_struct; 
set(gcf,'userdata',figure_data); 

enter image description here

希望這會有所幫助。

+0

再次感謝il_raffa!我仍然在慢慢地通過代碼,但它確實與我想要做的很好的工作;)。 –

+0

不客氣!如果您需要更多的細節或解釋,請告訴我。 –