2014-11-03 104 views
0

我正在创建一个非常简单的GUI。 用户输入的一些参数由绘制图表的函数获取。 该函数使用另一个函数来计算我们所说的“d”的值。这些功能保存在不同的.m文件中。获取函数变量的值到GUI

我怎么能得到那个“d”的功能,并把它放在图形用户界面的某个地方后,点击按钮(例如在编辑文本)?

问题是我不能直接获取d,因为它存储在另一个文件中,我不知道从函数中获取它的命令。

感谢您的帮助!我希望它不是模糊的。

+1

你不能只是返回变量作为输出参数并传递它? – chappjc 2014-11-03 23:31:43

+0

非常感谢! 选项B是我真正需要的,尽管我希望在不改变ABCFunction的代码的情况下(添加“setappdata(0,'dInFunction',d)”)是可能的。 无论如何,它的工作,感谢一百万! – poniek 2014-11-04 09:21:24

+0

不客气@poniek。请考虑upvoting /接受我的答案,如果它解决了你的问题。感谢和高兴它的工作! – 2014-11-04 14:35:10

回答

1

我认为@chappjc提供的选项是获得价值的最佳方式。我建议另一种方法,您可能会发现有用,使用setappdata和getappdata。这些用于在某些工作空间(例如,基本工作空间或特定功能的工作空间)中存储变量,并可用于在功能之间共享数据。在这个例子中,我使用了Matlab的根,即'base'工作区。

在下面的代码中有两个函数,一个叫做ABCFunction,它计算d和一个叫做ABCGUI的函数,它是一个简单的GUI来演示这个点。基本上用户在3个编辑文本框中输入3个值,按下按钮后,通过功能'ABCFunction'计算d,输出显示在编辑框中。

chappjc提出的方法称为选项A,使用setappdata/getappdata的选项是选项B.您可以在两个函数中注释适当的代码以便四处播放并查看它是如何工作的。结果是一样的。所以,在这里,他们是:

1) ABC功能

function d = ABCFunction(a,b,c) 

%// Option A 
d = a*b+c; 

%//Option B 
d = a*b+c; %// Does not change from Option A. 
setappdata(0,'dInFunction',d); %// Assign the result, d, in a variable that you will call from your main GUI. See below. 
end 

2) ABCGui功能

function ABCGui(~) 

% Create the GUI figure. 
handles.figure = figure('Visible','on','Position',[360,500,300,285]); 

handles.texta = uicontrol('Style','text','String','Enter a',... 
    'Position',[50,140,60,15]); 

handles.edita = uicontrol('Style','edit','String','',... 
    'Position',[50,120,60,20]); 


handles.textb = uicontrol('Style','text','String','Enter b',... 
    'Position',[120,140,60,15]); 

handles.editb = uicontrol('Style','edit','String','',... 
    'Position',[120,120,60,20]); 


handles.textc = uicontrol('Style','text','String','Enter c',... 
    'Position',[190,140,60,15]); 

handles.editc = uicontrol('Style','edit','String','',... 
    'Position',[180,120,60,20]); 

handles.Button = uicontrol('Style','pushbutton','String','Calculate d','Position',[50,60,80,15],'Callback',@PushbuttonCallback); 

handles.textd = uicontrol('Style','text','String','d = a*b+c',... 
    'Position',[80,90,80,15]); 

handles.textResult = uicontrol('Style','text','String','',... 
    'Position',[175,90,60,15]); 

guidata(handles.figure,handles) %// Update handles structure. 
%============================================================ 

    %// Setup callback of the pushbutton 
    function PushbuttonCallback(~,~) 

     handles = guidata(gcf); %// Retrieve handles structure 

     A = str2num(get(handles.edita,'String')); 
     B = str2num(get(handles.editb,'String')); 
     C = str2num(get(handles.editc,'String')); 

     %// Option A 
     d = ABCFunction(A,B,C); %// Call the function and assign the output directly to a variable. 

     %// Option B 
     ABCFunction(A,B,C); %// Call the function and fetch the variable using getappdata. 
     d = getappdata(0,'dInFunction'); 

     set(handles.textResult,'String',num2str(d)); 

    end 
end 

图形用户界面非常简单,看起来像下面这样:

enter image description here

正如您所看到的,将变量分配给函数输出要简单得多。如果这是不可能的,你可以选择选项B.希望有帮助!

+0

他不会,但我会。 ;)希望我也能接受。 – chappjc 2014-11-04 18:14:19

+0

哈哈哈谢谢! :) – 2014-11-04 18:17:28