所以我在指南中创建了GUI,看起来像这样:
gui
我想从广播按钮访问数据,然后更改模拟中的变量(比特率和调制是按钮组,改进是一个单个单选按钮(。例如 - 在模拟中,我有一个可变的Rs=1e9
,因此,当选择1Gbps按钮时,我希望它保留1E9,但是如果选择了10GBPS按钮,我希望它将其值更改为10E9。
然后击中启动按钮后,我想用给定参数启动模拟(以不同的.m文件为单位(。我该怎么做?(我知道MATLAB中的Handles Ideas,但我不知道如何将价值传递给模拟(
那是控制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
您可以使用对象" 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
更新
在您的情况下,您可以使用另一种方法来执行此操作。按下按钮启动时,您会读取无线电按钮的状态,然后将适当的值传递给我认为它称为"仿真"的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)');