我的popupmenu有不同的情况。但是,我希望有一些文件(例如.mat)从中读取案例(例如for循环?)。这怎么可能实现?
% --- Executes on selection change in hd_poweramp.
function hd_poweramp_Callback(hObject, eventdata, handles)
% hObject handle to hd_poweramp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
val = get(hObject, 'Value');
str = get(hObject, 'String');
switch str{val}
case '325LA Precision Acoustics'
if handles.transducer_index == 1;
handles.fgen1_voltage = (handles.fgen1_PNP*2-28.8)/0.72; % Double check that
else if handles.transducer_index == 2;
handles.fgen1_voltage = (handles.fgen1_PNP*2-28.8)/6.055; % Double check that
end
end
case '2100L E&I'
if handles.transducer_index == 1;
handles.fgen1_voltage = (handles.fgen1_PNP*2-10.267)/1.6; % Double check that
else if handles.transducer_index == 2;
handles.fgen1_voltage = (handles.fgen1_PNP*2-28.8)/6.055; % Double check that
end
end
end
guidata(hObject, handles); % Update the GUI data structure
答案 0 :(得分:0)
您可以将数据存储在统一的结构中,并使用它来生成下拉选项并推动计算。
考虑以下示例GUI(底部的完整代码):
以下是两个最相关的部分:
function makedummydata(mydatafile)
cases(1).Name = '325LA Precision Acoustics';
cases(1).ntransducers = 2;
cases(1).TransducerConstant1(1) = 28.8;
cases(1).TransducerConstant1(2) = 28.8;
cases(1).TransducerConstant2(1) = 0.72;
cases(1).TransducerConstant2(2) = 6.055;
cases(2).Name = '2100L E&I';
cases(2).ntransducers = 2;
cases(2).TransducerConstant1(1) = 10.267;
cases(2).TransducerConstant1(2) = 28.8;
cases(2).TransducerConstant2(1) = 1.6;
cases(2).TransducerConstant2(2) = 6.055;
save(mydatafile, 'cases');
end
和
function recalc(hObj, ~)
handles = guidata(hObj);
deviceID = handles.devicedd.Value;
transducerID = handles.transducerdd.Value;
fgen1_PNP = str2double(handles.fgen1_PNP.String);
TransducerConstant1 = handles.UserData(deviceID).TransducerConstant1(transducerID);
TransducerConstant2 = handles.UserData(deviceID).TransducerConstant2(transducerID);
voltage = (fgen1_PNP*2 - TransducerConstant1)/TransducerConstant2;
handles.resultsbox.String = voltage;
end
在这里,我创建了一个结构数组,其中每个顶级索引对应于一个设备及其相关常量。生成GUI时,下拉列表将填充所有设备名称,并且传感器列表中填充了设备可用的传感器数量。
当您更改其中一个下拉列表或fgen1 PNP时,将重新计算电压。注意重新计算功能有多简单。因为我们以MATLAB-ic方式设置数据结构,所以我们可以轻松访问所有相关部分,而无需循环,字符串操作或可怕的eval
。
完整的GUI代码:
function testcode
% Set up sample data
mydatafile = 'mydevices.mat';
makedummydata(mydatafile);
buildGUI(mydatafile);
end
function recalc(hObj, ~)
handles = guidata(hObj);
deviceID = handles.devicedd.Value;
transducerID = handles.transducerdd.Value;
fgen1_PNP = str2double(handles.fgen1_PNP.String);
TransducerConstant1 = handles.UserData(deviceID).TransducerConstant1(transducerID);
TransducerConstant2 = handles.UserData(deviceID).TransducerConstant2(transducerID);
voltage = (fgen1_PNP*2 - TransducerConstant1)/TransducerConstant2;
handles.resultsbox.String = voltage;
end
function makedummydata(mydatafile)
cases(1).Name = '325LA Precision Acoustics';
cases(1).ntransducers = 2;
cases(1).TransducerConstant1(1) = 28.8;
cases(1).TransducerConstant1(2) = 28.8;
cases(1).TransducerConstant2(1) = 0.72;
cases(1).TransducerConstant2(2) = 6.055;
cases(2).Name = '2100L E&I';
cases(2).ntransducers = 2;
cases(2).TransducerConstant1(1) = 10.267;
cases(2).TransducerConstant1(2) = 28.8;
cases(2).TransducerConstant2(1) = 1.6;
cases(2).TransducerConstant2(2) = 6.055;
save(mydatafile, 'cases');
end
function buildGUI(mydatafile)
% Generate GUI
h.f = figure('MenuBar', 'none', 'ToolBar', 'none', 'NumberTitle', 'off');
cases = load(mydatafile, 'cases');
h.UserData = cases.cases; % Remove extra layer
h.lbl(1) = uicontrol('Parent', h.f, 'Style', 'Text', ...
'Units', 'Normalized', 'Position', [0.1 0.7 0.6 0.1], ...
'FontSize', 12, 'HorizontalAlignment', 'left', 'String', 'Device Thing');
h.devicedd = uicontrol('Parent', h.f, 'Style', 'popupmenu', ...
'Units', 'Normalized', 'Position', [0.1 0.55 0.6 0.2], ...
'String', {h.UserData(:).Name}, 'Callback', @recalc);
h.lbl(2) = uicontrol('Parent', h.f, 'Style', 'Text', ...
'Units', 'Normalized', 'Position', [0.1 0.5 0.6 0.1], ...
'FontSize', 12, 'HorizontalAlignment', 'left', 'String', 'Transducer Thing');
h.transducerdd = uicontrol('Parent', h.f, 'Style', 'popupmenu', ...
'Units', 'Normalized', 'Position', [0.1 0.35 0.6 0.2], ...
'String', 1:h.UserData(1).ntransducers, 'Callback', @recalc);
h.lbl(3) = uicontrol('Parent', h.f, 'Style', 'Text', ...
'Units', 'Normalized', 'Position', [0.1 0.3 0.3 0.1], ...
'FontSize', 12, 'HorizontalAlignment', 'left', 'String', 'fgen1 PNP Thing');
h.fgen1_PNP = uicontrol('Parent', h.f, 'Style', 'edit', ...
'Units', 'Normalized', 'Position', [0.1 0.25 0.25 0.1], ...
'String', 1);
h.lbl(4) = uicontrol('Parent', h.f, 'Style', 'Text', ...
'Units', 'Normalized', 'Position', [0.5 0.3 0.3 0.1], ...
'FontSize', 12, 'HorizontalAlignment', 'left', 'String', 'Result');
h.resultsbox = uicontrol('Parent', h.f, 'Style', 'edit', ...
'Units', 'Normalized', 'Position', [0.5 0.25 0.25 0.1]);
guidata(h.f, h);
recalc(h.f, 'hi'); % Fire first calc
end