将单选按钮中的值传递到Matlab中的脚本中

时间:2017-06-08 06:28:38

标签: matlab matlab-guide matlab-gui

所以我在指南中创建了GUI,如下所示:

GUI

我想从单选按钮访问数据,然后在模拟中更改变量(比特率和调制是按钮组,改进是单个单选按钮)。例如 - 在模拟中我有一个变量Rs=1e9,所以当选择1Gbps按钮时我希望它保持1e9,但如果选择了10Gbps按钮,我希望它将其值改为10e9。

然后在点击“开始”按钮后,我想用给定的参数开始我的模拟(在不同的.m文件中)。 我该怎么做?(我知道matlab中的句柄想法,但我不知道如何将值传递给模拟)

控制gui的代码 - 由指南生成。我添加了一些启动模拟并关闭gui窗口的代码。

function varargout = gui_final(varargin)


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_final_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_final_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 gui_final is made visible.
function gui_final_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = gui_final_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

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


% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject    handle to start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc
close all

message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
uiwait(msgbox(message));
evalin('base', 'simulation');


% --- Executes on button press in dfe.
function dfe_Callback(hObject, eventdata, handles)
% hObject    handle to dfe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of dfe


% --- Executes on button press in ook.
function ook_Callback(hObject, eventdata, handles)
% hObject    handle to ook (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of ook

1 个答案:

答案 0 :(得分:0)

您可以使用对象“hObject”

来完成

像这样:假设你有一个滑块,每次移动滑块时都会调用回调。您可以使用它来存储变量。然后,当您按下按钮时,您想要使用该数据。代码是:

function slider_callback(hObject,eventdata)
    sval = hObject.Value;
    diffMax = hObject.Max - sval;
    data = struct('val',sval,'diffMax',diffMax);
    hObject.UserData = data;
    % For R2014a and earlier: 
    % sval = get(hObject,'Value');  
    % maxval = get(hObject,'Max');  
    % diffMax = maxval - sval;      
    % data = struct('val',sval,'diffMax',diffMax);   
    % set(hObject,'UserData',data);   

end

function button_callback(hObject,eventdata)
    h = findobj('Tag','slider1');
    data = h.UserData;
    % For R2014a and earlier: 
    % data = get(h,'UserData'); 
    display([data.val data.diffMax]);
end

参考:Matlab Docs

更新

在您的情况下,如果您愿意,可以使用其他方法。当您按START按钮时,您将读取单选按钮的状态,并将适当的值传递给您认为称为“模拟”的simulation_function。在下面的例子中,我假设你有一个名为(tag)的按钮组:uibuttongroup1,里面有两个按钮:radiobutton1和radiobutton2

% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject    handle to start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%Check which radio button is pressed
switch get(get(handles.uibuttongroup1,'SelectedObject'),'Tag')
  case 'radiobutton1',  myParameter = 1e9;
  case 'radiobutton2',  myParameter = 10e9;
end
%Execute simulation
clc
close all

message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
uiwait(msgbox(message));
evalin('base', 'simulation(myParameter)');