利用界面将workspace中变量导入函数或gui界面
大家做界面的时候应该遇到过这样的问题,如何通过界面读取工作窗口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',,...
'menubar','none','name','选择工作窗口变量','resize','off',...
'numbertitle','off','color',,'tag','bianliangselect');
tubiao = imread('tubiao.jpg');
uicontrol(VarSelect_fig,'style','radiobutton','units','normalized',...
'pos',,'string','',...
'cdata',tubiao,'bac',)
uicontrol(VarSelect_fig,'style','text','units','normalized',...
'pos',,'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',,'string',...
bianliangname,'fontsize',font_size1,'fontunits','normalized',...
'backgroundcolor',,'tag','bianliang_name','value',1)
uicontrol(VarSelect_fig,'style','push','units','normalized','pos',,'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',,'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); 楼主太强了,建议版大加分。
实现这个功能还是很有意义的。
只是以前主要是编写函数程序,几乎没有用到对工作空间workspace中的变量的引用。
学习了!
最关键的是:s=evalin('base','whos');吧
evalin和assignin很有用
我做的这个界面只是选择double类型的一维数据,只需作简单修改就可以选择workspace中所有变量。 谢谢楼主的心血虽然个人没试过, GUI太久没用了!
但直觉这个不错, 建议常玩GUI或有兴趣者可试试! 刚开始学的菜鸟...过来学习一下~
页:
[1]