将外部变量传递给MATLAB GUIDE

时间:2013-09-11 17:46:32

标签: matlab function variables matlab-guide

我的程序从文件加载数据并生成图表,用户点击感兴趣的区域,然后完成分析并生成新图表。程序继续要求用户单击图像,直到用户按下e退出程序。

我希望生成的图形是一个从我的程序中获取数据的GUI,但我似乎无法将这些数据传输到GUI函数中。以下是我的程序的快速示例:

load(data)
plot(x,y)
while 1%so that it continues asking for user interaction
     figure(1)
     'click on the point you want or press e to exit'
     [x1,y1,key]=ginput(1)

     f=score(x1,y1)
     %the above is a different function that gives us the data that I want to graph,
     %that are called xf,yf 

     %GUI plot
     figure(1)
     test_gui(xf,yf)

     if (key == 'e')
     display('End')
     break;
     else
     display('next point')
     end
end

我的test_gui.m看起来像这样:

function varargout = test_gui(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
               'gui_Singleton',  gui_Singleton, ...
               'gui_OpeningFcn', @test_gui_OpeningFcn, ...
               'gui_OutputFcn',  @test_gui_OutputFcn, ...
               'gui_LayoutFcn',  [] , ...
               'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before fft_guide is made visible.
function test_gui_OpeningFcn(hObject, eventdata, handles, varargin)


% Choose default command line output for test_gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = test_gui_OutputFcn(hObject, eventdata, handles) 

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
plot (xf,yf)   

问题在于,当我点击“推送”按钮时,它没有绘制任何图形,因此我传递xfyf变量的方式一定有问题。我想知道是否有人对我做错了什么有任何想法,我之前没有使用过GUIDE,而且似乎我迷路了。

1 个答案:

答案 0 :(得分:0)

从代码的外观来看,永远不会定义xfyf,只会fscore的结果)。这就是为什么你看不到任何阴谋的原因。

假设scorexfyf转储到工作区,您必须先从varargin定义它们,然后使用{{1}将它们传递给回调函数,正如沃纳评论的那样。

handles

在回调中:

% --- Executes just before fft_guide is made visible.
function test_gui_OpeningFcn(hObject, eventdata, handles, varargin)
xf = varargin{0}; yf = varargin{1}; % Get xf and yf from input
handles.xf = xf; handles.yf = yf;  % Put the values in handles
guidata(hObject,handles);   % Save handles so you can use it anywhere in the GUI

我认为这应该有效,假设% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) plot (handles.xf,handles.yf) xf在传递给GUI函数之前正确定义。