2012-10-03 288 views
2

我做了一個GUI,獲取2個元素:文件(文件列表)和選項(我將在後面解釋)。matlab:關閉GUI如果用戶按下關閉按鈕

有調用下面的功能的功能:

// asume that this is my 'options' 
options{1} = {'Treatment', 't1', 't2'}; 
options{2} = {'Tree', '1', '2'}; 
options{3} = {'Replica', 'r1', 'r2', 'r3'}; 

// asume that this is my 'files' (may be more than 2 images 
files{1} = 'C:\Documents and Settings\erezalon\Desktop\gib_proj_02.10.12\fina3_32bit\IMG_4640.JPG'; 
files{2} = 'C:\Documents and Settings\erezalon\Desktop\gib_proj_02.10.12\fina3_32bit\grapes03.jpg'; 


mydata = mainGUI(options, files). 

// here I want to check 'mydata'. if it is equal to zero or not. 

對於每個圖像,在函數「mainGUI」(但每次創建GUI,一個GUI被顯示,直到用戶按下「下一「)。

我想要做的下一件事:

如果用戶在GUI中按下關閉按鈕,我想阻止其他GUI-S的顯示和設置「數據= 0」。然後,我將檢查返回的元素('mydata')是什麼,如果它等於0,我會知道用戶關閉了GUI,我將根據需要採取行動。

我試圖通過'cmdClose_Callback'來做到這一點,但它不起作用。

假設'文件'=兩個圖像,所以這是第一個圖像的GUI。

我按下關閉按鈕:

enter image description here

,並獲得第二圖像的GUI,儘管封閉第一圖像的圖形用戶界面。

enter image description here

我想,當我關閉GUI,其他GUI-S不會出現。

這是我的代碼:

function data = mainGUI(options, files) 
%# current options 
j = 1; 
ops = cellfun(@(c) c(1), options, 'Uniform',false); 
data{j} = [ops{1:length(ops)}]; 
j = j + 1; 


options = cellfun(@(c) c(2:1:end), options, 'Uniform',false); 
clear ops; 
ops = cellfun(@(c) c(1), options, 'Uniform',false); 
opts = [ops{1:length(ops)}]; 

%# create main figure, with plot and options button 
hFig = figure('Name','window 1','Visible','Off'); 
a = 1 
callback 

%# options button callback function 
function callback(o,e) 
    a = 2 
    %# save current options (sharing data between the two GUIs) 
    setappdata(hFig, 'opts',opts); 

    %# display options dialog and wait for it 

    for k=1: length(files) 

      hOptsGUI = secondaryGUI(hFig, options, k, length(files)); 

      img = imread(files{k}); %# Read the data from image file data 
      hAxes = axes('Parent',hOptsGUI,'Units','pixels','Position',[362 242 424 359]); %# so the position is easy to define 
      image(img,'Parent',hAxes); %# Plot the image 
      set(hAxes,'Visible','off');   %# Turn the axes visibility off 

      a = 3 

      waitfor(hOptsGUI); 

      a = 4 
      %# get new options, and update plot accordingly 
      opts = getappdata(hFig, 'opts'); 
      data{j} = opts; 
      j = j + 1; 
    end 
end 

end 

function hFig = secondaryGUI(hParentFig, options, k, num_files) 
%# create figure 

a = 5 


hFig = figure('Name','Step 3 of 4: Choose data for each image','Menubar','none', 'Resize','off', ... 
    'WindowStyle','modal', 'Position',[300 300 1150 600], 'CloseRequestFcn',@cmdClose_Callback); 
set(gcf,'NumberTitle','off') 
movegui(hFig, 'center'); 



options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false); 
num = length(options); 

%# get saved settings 
selected = getappdata(hParentFig, 'opts'); 
a = 6 
%# top/bottom panels 
hPanBot = uipanel('Parent',hFig, 'BorderType','none', ... 
    'Units','normalized', 'Position',[0 0.0 1 0.2]); 

hPanTop = uipanel('Parent',hFig, 'BorderType','none', ... 
    'Units','normalized', 'Position',[0 0.2 1 0.2]); 



%# buttongroups in top panel 
hBtnGrp = zeros(1,num); 
width = 1/num; 
for i=1:num 
    %# create button group 
    hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ... 
     'Units','normalized', 'Position',[(i-1)*width 0 width 1]); 
    %# populate it with radio buttons 
    height = 1./numel(options{i}); 
    for j=1:numel(options{i}) 
     h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ... 
      'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ... 
      'String',options{i}{j}); 
     %# set initially selected values 
     if strcmp(selected{i},options{i}{j}) 
      set(hBtnGrp(i), 'SelectedObject',h) 
     end 
    end 
end 

if k ~= num_files 
%# save button in bottom panel 
uicontrol('Parent',hPanBot, 'Style','pushbutton', ... 
    'Units','normalized', 'Position',[0.3 0.2 0.4 0.2], ... 
    'String','next', 'Callback',@callback); 

else 
    uicontrol('Parent',hPanBot, 'Style','pushbutton', ... 
    'Units','normalized', 'Position',[0.3 0.2 0.4 0.2], ... 
    'String','start', 'Callback',@callback); 
end 


%# save button callback function 
function callback(o,e) 
    a = 7 
    %# get selected values 
    hObjs = get(hBtnGrp(:), 'SelectedObject'); 
    vals = get(cell2mat(hObjs),{'String'}); 

    %# update settings 
    setappdata(hParentFig, 'opts',vals); 


    close(hFig) 
    a = 8 
end 

function cmdClose_Callback(hObject,varargin) 
    disp(['Close Request coming from: ',get(hObject,'Type')]); 
    a = 9 

    %do cleanup here 
    delete(hFig); 
    a = 10 

end 
end 

如果「文件」有兩個圖像,我推「下一步」按鈕,第一個數字,並在第二個數字我關閉,我得到:

a = 1 
a = 2 
a = 5 
a = 6 
a = 3 
a = 7 
Close Request coming from: figure 
a = 9 
a = 10 
a = 8 
a = 4 
a = 5 
a = 6 
a = 3 
Close Request coming from: figure 
a = 9 
a = 10 
a = 4 

上面的行:hOptsGUI = secondaryGUI(hFig,options,k,length(files)); 我試圖把一些線。爲了testthe行,我打印一個合適的消息:

if (ishandle(hFig)) 
    disp('exists'); 
else disp('was closed'); 
end; 

,但它不工作:/

每個GUI用戶將關閉,接下來的回調將被稱爲:

function callback(o,e) 

    %# get selected values 
    hObjs = get(hBtnGrp(:), 'SelectedObject'); 
    vals = get(cell2mat(hObjs),{'String'}); 

    %# update settings 
    setappdata(hParentFig, 'opts',vals); 

    %# close options dialog 
    close(hFig) 

end 

,所以我只需要知道在未來的「for循環」如果這個回調被稱爲:

for k=1: length(files) 

我該怎麼做?

+0

'delete(hFig)'幫助你嗎? – Derek

+0

不,它不會:/我試圖把刪除(hfig)放在mainGUI函數中的'callback'這一行。感謝你的努力。 –

回答

1

我成功了!

我使用setappdata和getappdata爲了知道第二個回調是否被調用。

@Derek,德里克,非常感謝你的幫助,你花了很多時間給我!

function data = mainGUI(options, files) 
%# current options 
j = 1; 
ops = cellfun(@(c) c(1), options, 'Uniform',false); 
data{j} = [ops{1:length(ops)}]; 
j = j + 1; 


options = cellfun(@(c) c(2:1:end), options, 'Uniform',false); 
clear ops; 
ops = cellfun(@(c) c(1), options, 'Uniform',false); 
opts = [ops{1:length(ops)}]; 

%# create main figure, with plot and options button 
hFig = figure('Name','window 1','Visible','Off'); 
callback 


%# options button callback function 
function callback(o,e) 
    %# save current options (sharing data between the two GUIs) 
    setappdata(hFig, 'opts',opts); 

    %# display options dialog and wait for it 

    for k=1: length(files) 
      hOptsGUI = secondaryGUI(hFig, options, k, length(files)); 

      img = imread(files{k}); %# Read the data from your image file 
      hAxes = axes('Parent',hOptsGUI,'Units','pixels','Position',[362 242 424 359]); %# so the position is easy to define 
      image(img,'Parent',hAxes); %# Plot the image 
      set(hAxes,'Visible','off');   %# Turn the axes visibility off 

      out = 'FALSE'; 
      setappdata(hFig,'some_var',out); 
      % show the images 
      %%Im = imread(files{k}); 
      %%AxesH = axes('Units', 'pixels', 'position', [0.5, 10, 400, 260], 'Visible', 'off'); 
      %%image('Parent', AxesH, 'CData', Im); %# add other property-value pairs as needed 

      waitfor(hOptsGUI); 

      some_other_var = getappdata(hFig,'some_var'); 

      if (strcmp(some_other_var, 'OK') == 1) 
       %# get new options, and update plot accordingly 
       opts = getappdata(hFig, 'opts'); 
       data{j} = opts; 
       j = j + 1; 
      else 
       k = length(files); 
       data = 0; 
       return; 
      end; 
    end 
end 
end 

function hFig = secondaryGUI(hParentFig, options, k, num_files) 
%# create figure 


hFig = figure('Name','Step 3 of 4: Choose data for each image','Menubar','none', 'Resize','off', ... 
    'WindowStyle','modal', 'Position',[300 300 1150 600]); 
set(gcf,'NumberTitle','off') 
movegui(hFig, 'center'); 



options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false); 
num = length(options); 

%# get saved settings 
selected = getappdata(hParentFig, 'opts'); 

%# top/bottom panels 
hPanBot = uipanel('Parent',hFig, 'BorderType','none', ... 
    'Units','normalized', 'Position',[0 0.0 1 0.2]); 

hPanTop = uipanel('Parent',hFig, 'BorderType','none', ... 
    'Units','normalized', 'Position',[0 0.2 1 0.2]); 



%# buttongroups in top panel 
hBtnGrp = zeros(1,num); 
width = 1/num; 
for i=1:num 
    %# create button group 
    hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ... 
     'Units','normalized', 'Position',[(i-1)*width 0 width 1]); 
    %# populate it with radio buttons 
    height = 1./numel(options{i}); 
    for j=1:numel(options{i}) 
     h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ... 
      'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ... 
      'String',options{i}{j}); 
     %# set initially selected values 
     if strcmp(selected{i},options{i}{j}) 
      set(hBtnGrp(i), 'SelectedObject',h) 
     end 
    end 
end 

if k ~= num_files 
%# save button in bottom panel 
uicontrol('Parent',hPanBot, 'Style','pushbutton', ... 
    'Units','normalized', 'Position',[0.3 0.2 0.4 0.2], ... 
    'String','next', 'Callback',@callback); 
else 
    uicontrol('Parent',hPanBot, 'Style','pushbutton', ... 
    'Units','normalized', 'Position',[0.3 0.2 0.4 0.2], ... 
    'String','start', 'Callback',@callback); 
end 
%# save button callback function 
function callback(o,e) 
    out = 'OK'; 
    setappdata(hParentFig,'some_var',out); 
    %# get selected values 
    hObjs = get(hBtnGrp(:), 'SelectedObject'); 
    vals = get(cell2mat(hObjs),{'String'}); 

    %# update settings 
    setappdata(hParentFig, 'opts',vals); 

    %# close options dialog 
    close(hFig) 
end 

function cmdClose_Callback(hObject,varargin) 
    %do cleanup here 
    delete(hFig); 
end 
end 
+0

我很高興你明白了! – Derek

0

沒有呼叫從第一個GUI關閉圖形。

您可能需要添加一個呼叫close(hFig)mainGUI函數的末尾。

+0

它不起作用,所以我正在編輯我的主題..謝謝! –

2

只需檢查hFig1在創建第二個數字之前是否有效。

if ishandle(hFig1) 
    hFig2 = figure(...) 
else 
    return;  % do something else 
end 

根據需要重複。


function data = mainGUI(options, files) 
    % ... 
    % create main figure, with plot and options button 
    hFig = figure('Name','window 1','Visible','Off'); 

    complete = callback; 
    if complete == 1 
     data = data; 
    else 
     data = 0; 
    end 

% options button callback function 
function complete = callback(o,e) 
    %save current options (sharing data between the two GUIs) 
    setappdata(hFig, 'opts',opts); 

    % display options dialog and wait for it 
    complete = 0; 
    for k = 1 : length(files) 
     hOptsGUI = secondaryGUI(hFig, options, k, length(files)); 
     % ... 
    end 
    % we reached the end of the loop! 
    complete = 1; 
end 
+0

你確定嗎?我認爲這不是一個有效的句柄,因爲句柄可以通過「關閉」按鈕或用戶按下「下一個」按鈕關閉。 –

+0

@AlonShmiel不知道我是否明白你想要做什麼。查看更新的代碼。 – Derek

+0

@AlonShmiel如果圖1被用戶關閉,那麼我們不會創建圖2.這是你想要的嗎? – Derek