0

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

图形用户界面

我想从单选按钮访问数据,然后更改模拟中的变量(比特率和调制是按钮组,改进是单个单选按钮)。For example - in the simulation I have a variable Rs=1e9, so when 1Gbps button is selected I want it to remain 1e9, but if 10Gbps button is selected I want it to change its value to 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
4

1 回答 1

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 文档

更新

在你的情况下,如果你愿意,你可以用另一种方法来做到这一点。当您按下按钮开始时,您会读取单选按钮的状态,并将适当的值传递给您的模拟函数,我认为它称为“模拟”。在下面的示例中,我假设您有一个名为 (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)');
于 2017-06-08T06:39:52.953 回答