从文件中读取案例

Reading cases from a file

我的弹出菜单导致了不同的情况。但是,我希望有某种文件(例如 .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

您可以将数据存储在统一的结构中,并使用它来生成下拉选择并驱动计算。

考虑以下示例 GUI(底部的完整代码):

以下是最相关的 2 个部分:

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