Matlab gui returns 输出参数太快
Matlab gui returns output arguments too soon
虽然我是一个相当中等的 matlab 用户,但我无法让我的 gui 工作。它的目的是让我使用一种单击方式来组织我的下载,但它 return 输出参数的方式太快了。
代码如下:
function [answer]=makegui(files,season,episode)
close all
%% Initialize variables
% Input (if empty)
if nargin==0
files{1}='Avengers';
files{2}='NCIS';
files{3}='Would I Lie to You';
season{1}=1;
season{2}=2;
season{3}=3;
episode{1}=11;
episode{2}=22;
episode{3}=24;
else
if length(files)>length(season) || length(files)>length(episode)
sprintf('Not enough filenames provided')
delete(fig)
end
end
width=450;
height=100+25*length(files);
% Function variables
checks=ones(1,length(files));
% Output variables
answer=[];
fig = figure('Name','Fili3','NumberTitle','off','Visible','off','Position',[360,500,width,height],'CloseRequestFcn',@temp); % Create figure
%% Declare buttons
all = uicontrol('Style','checkbox',...
'String','Select all','Position',[50,height-35,70,25],...
'Callback',{@all_Callback},'Value',1);
for i=1:length(files)
item{i}=uicontrol('Style','checkbox',...
'String',files{i},'Position',[75,height-60-25*i,150,25],...
'Callback',@item_Callback,'Value',1);
uicontrol('Style','text','String',sprintf('%d',season{i}),...
'Position',[320,height-60-25*i,25,25]);
uicontrol('Style','text','String',sprintf('%d',episode{i}),...
'Position',[370,height-60-25*i,25,25]);
end
uicontrol('Style','text','String','TV Show Name',...
'Position',[90,height-60,60,15]);
uicontrol('Style','text','String','Season',...
'Position',[300,height-60,60,15]);
uicontrol('Style','text','String','Episode',...
'Position',[350,height-60,60,15]);
options = uicontrol('Style','popupmenu',...
'String',{'Rename and Move','Rename','Move'},...
'Position',[300,height-35,120,25],...
'Value',1);
uicontrol('Style','pushbutton','String','Run',...
'Position',[345,12,30,30],'Callback',@closefunction);
% Make UI visible
fig.Visible='on';
movegui(fig,'center')
%% Callback functions
function all_Callback(source,eventdata)
if source.Value
checks=ones(1,length(files));
else
checks=zeros(1,length(files));
end
source.Value
for j=1:length(files)
item{j}.Value=source.Value;
end
end
function item_Callback(source,eventdata)
S=regexp(files,source.String);
i=find(~cellfun(@isempty,S));
checks(i)=source.Value;
if source.Value==0
all.Value=0;
elseif source.Value==1 && sum(checks)==length(files)
all.Value=1;
end
end
function closefunction(source,eventdata)
answer.checks=checks(:);
answer.mode=options.Value;
delete(fig)
end
function temp(source,eventdata)
delete(fig)
end
end
我添加了nargin部分,以便在测试时不必经常提供输入参数,后来我打算让运行按钮和关闭功能指向相同的class。我希望它 return 回答,但是如果你 运行 在你自己的计算机上这样做,你会看到它 return 在答案初始化后就立即回答,然后拒绝做任何事情否则。
这可能只是我忘记的一小段代码,但如果有人能为我指出正确的方向,那就太好了!
谢谢,
汤姆
知道了!只需要添加 waitfor(fig).
虽然我是一个相当中等的 matlab 用户,但我无法让我的 gui 工作。它的目的是让我使用一种单击方式来组织我的下载,但它 return 输出参数的方式太快了。 代码如下:
function [answer]=makegui(files,season,episode)
close all
%% Initialize variables
% Input (if empty)
if nargin==0
files{1}='Avengers';
files{2}='NCIS';
files{3}='Would I Lie to You';
season{1}=1;
season{2}=2;
season{3}=3;
episode{1}=11;
episode{2}=22;
episode{3}=24;
else
if length(files)>length(season) || length(files)>length(episode)
sprintf('Not enough filenames provided')
delete(fig)
end
end
width=450;
height=100+25*length(files);
% Function variables
checks=ones(1,length(files));
% Output variables
answer=[];
fig = figure('Name','Fili3','NumberTitle','off','Visible','off','Position',[360,500,width,height],'CloseRequestFcn',@temp); % Create figure
%% Declare buttons
all = uicontrol('Style','checkbox',...
'String','Select all','Position',[50,height-35,70,25],...
'Callback',{@all_Callback},'Value',1);
for i=1:length(files)
item{i}=uicontrol('Style','checkbox',...
'String',files{i},'Position',[75,height-60-25*i,150,25],...
'Callback',@item_Callback,'Value',1);
uicontrol('Style','text','String',sprintf('%d',season{i}),...
'Position',[320,height-60-25*i,25,25]);
uicontrol('Style','text','String',sprintf('%d',episode{i}),...
'Position',[370,height-60-25*i,25,25]);
end
uicontrol('Style','text','String','TV Show Name',...
'Position',[90,height-60,60,15]);
uicontrol('Style','text','String','Season',...
'Position',[300,height-60,60,15]);
uicontrol('Style','text','String','Episode',...
'Position',[350,height-60,60,15]);
options = uicontrol('Style','popupmenu',...
'String',{'Rename and Move','Rename','Move'},...
'Position',[300,height-35,120,25],...
'Value',1);
uicontrol('Style','pushbutton','String','Run',...
'Position',[345,12,30,30],'Callback',@closefunction);
% Make UI visible
fig.Visible='on';
movegui(fig,'center')
%% Callback functions
function all_Callback(source,eventdata)
if source.Value
checks=ones(1,length(files));
else
checks=zeros(1,length(files));
end
source.Value
for j=1:length(files)
item{j}.Value=source.Value;
end
end
function item_Callback(source,eventdata)
S=regexp(files,source.String);
i=find(~cellfun(@isempty,S));
checks(i)=source.Value;
if source.Value==0
all.Value=0;
elseif source.Value==1 && sum(checks)==length(files)
all.Value=1;
end
end
function closefunction(source,eventdata)
answer.checks=checks(:);
answer.mode=options.Value;
delete(fig)
end
function temp(source,eventdata)
delete(fig)
end
end
我添加了nargin部分,以便在测试时不必经常提供输入参数,后来我打算让运行按钮和关闭功能指向相同的class。我希望它 return 回答,但是如果你 运行 在你自己的计算机上这样做,你会看到它 return 在答案初始化后就立即回答,然后拒绝做任何事情否则。
这可能只是我忘记的一小段代码,但如果有人能为我指出正确的方向,那就太好了!
谢谢, 汤姆
知道了!只需要添加 waitfor(fig).