马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
大家做界面的时候应该遇到过这样的问题,如何通过界面读取工作窗口workspace中的某个变量或某一类呢?关键是你可能事先不知道workspace中都有什么变量,你很想通过一个界面查看workspace中的变量,并做出选择。对此,我做了一个界面,供大家参考。我做的界面可以查看变量,做出选择,并返回变量的值。有了这个界面,就可以很方便的实现界面与工作窗口workspace的数据交换。不罗嗦了,下面就是代码。
function xie_vardata=VarSelect1
OldHandle = findobj( 'Type', 'figure', 'Tag', 'bianliangselect' ) ;
if ishandle( OldHandle )
close( OldHandle ) ;
end
VarSelect_fig = figure ;
scnsize=get(0,'screensize');
font_size1=12*sqrt(scnsize(3)*scnsize(4)/(1024*768));
set(VarSelect_fig,'units','normalized','position',[0.5 0.35 0.3 0.2],...
'menubar','none','name','选择工作窗口变量','resize','off',...
'numbertitle','off','color',[0.925 0.914 0.847],'tag','bianliangselect');
tubiao = imread('tubiao.jpg');
uicontrol(VarSelect_fig,'style','radiobutton','units','normalized',...
'pos',[0.025 0.3 0.265 0.48],'string','',...
'cdata',tubiao,'bac',[0.925 0.914 0.847])
uicontrol(VarSelect_fig,'style','text','units','normalized',...
'pos',[0.475 0.73 0.35 0.15],'string','选择变量',...
'fontsize',font_size1,'fontweight','bold','fontunits','normalized')
s=evalin('base','whos');
bianliangname=char(s.name);
if isempty(bianliangname)
bianliangname='当前窗口没有变量';
else
bianliangclass=char(s.class);
bianliangsize=[];
bianliangclasslog=[];
k=size(s,1);
for i=1:k
bianliangsize(i,:)=s(i).size;
bianliangclasslog(i,:)=strncmpi(bianliangclass(i,:),'double',6);
end
bianliangsize=min(bianliangsize')';
bianliangname=bianliangname(bianliangsize==1 & bianliangclasslog,:);
if isempty(bianliangname)
bianliangname='当前窗口没有变量';
end
end
setappdata(gcf,'bianliangname',bianliangname);
uicontrol(VarSelect_fig,'style','popupmenu','units','normalized','pos',[0.35 0.33 0.59 0.337],'string',...
bianliangname,'fontsize',font_size1,'fontunits','normalized',...
'backgroundcolor',[1 1 1],'tag','bianliang_name','value',1)
uicontrol(VarSelect_fig,'style','push','units','normalized','pos',[0.4 0.2 0.18 0.21],'string','确 定',...
'fontsize',font_size1,'fontweight','bold','fontunits','normalized','callback',...
['handles1=guidata(gcf);',...
'value=get(handles1.bianliang_name,''value'');'...
'bianliangname=getappdata(gcf,''bianliangname'');'...
'if ~strcmp(bianliangname,''当前窗口没有变量'');'...
'bs = bianliangname(value,:);'...
'xie_vardata=eval(bs);'...
'set(gcf,''userdata'',xie_vardata);'...
'end;'...
'uiresume(gcbf);'...
'evalin(''base'',''clear value bianliangname bs handles1 xie_vardata'');'])
uicontrol(VarSelect_fig,'style','push','units','normalized','pos',[0.7 0.2 0.18 0.21],'string','取 消',...
'fontsize',font_size1,'fontweight','bold','fontunits','normalized','callback',...
['set(gcf,''userdata'',[]);'...
'uiresume(gcbf);'...
'evalin(''base'',''clear value bianliangname bs handles1 xie_vardata'');'])
handles1=guihandles(gcf);
guidata(gcf,handles1);
uiwait(gcf);
xie_vardata=get(gcf,'userdata');
delete(gcf); |