2014-10-06 83 views
0

我在MATLAB GUIDE中构建了一个计算软件。我想要做的是在不同的编辑区域和一些下拉列表中填写所有的计算数据,当我按下计算“列表框”时应该填入文本“计算1”。将多个GUI数据集保存到列表框中,然后在MATLAB中再次加载它们

如果我再改变一些输入字段,然后按一定的数据计算,我再次想用“运算2”下方“运算1”等来填充列表框...

但我会想能够在列表框中再次高亮显示“计算1”,并按下“加载输入参数”按钮以使用计算“计算1”时使用的数据填充所有编辑输入字段。

我已经找遍了这个地方,但找不到任何东西。

// Robin

回答

1

下面是一些非常基本的代码,但它会执行您正在查找的内容。有很多可能的调整,但我会让你玩他们。我把解释作为评论。你可以将过去复制到Matlab中并根据需要更改GUI。

function CalculatorGUI 

% Dummy GUI to calculate A*B + C... 
clc 
clear 
close all 

global hTestResult hEditA hEditB hEditC CalculationList CalculationStrings 

% Set up controls 
CalculationList = nan(10,3); % Create array in which we store the parameters. 1st column is A, 2nd is B and 3rd is C. 
CalculationStrings = cell(10,1); 

ScreenSize = get(0,'ScreenSize'); 

hFig = figure('Visible','off','Position',[ScreenSize(3)/2,ScreenSize(4)/2,450,285]); 

hCalculateButton = uicontrol('Style','Pushbutton','Position',[350,150,80,30],'String','Calculate!','Callback',@CalculateCallback); 

hTitle = uicontrol('Style','Text','Position',[100,250,100,25],'String','Calculate (A * B) + C'); 

hTextA = uicontrol('Style','Text','Position',[125,220,70,25],'String','A'); 
hEditA = uicontrol('Style','Edit','Position',[125,200,70,25],'String','1'); 

hTextB = uicontrol('Style','Text','Position',[200,220,70,25],'String','B'); 
hEditB = uicontrol('Style','Edit','Position',[200,200,70,25],'String','2'); 

hTextC = uicontrol('Style','Text','Position',[275,220,70,25],'String','C'); 
hEditC = uicontrol('Style','Edit','Position',[275,200,70,25],'String','3'); 

hResultHeader = uicontrol('Style','Text','Position',[350,220,70,25],'String','Result'); 
hTestResult = uicontrol('Style','Text','Position',[350,200,70,25],'String',''); 


hTextCalcu = uicontrol('Style','Text','Position',[100,140,100,50],'String','Calculations'); 
hListCalcu = uicontrol('Style','Listbox','String','','Position',[100,120,200,50],'max',10,... 
    'min',1,'Callback',@ListBox_Callback); 

set(hFig,'Visible','on') 
%====================================================================== 
%====================================================================== 

% Callback of the pushbutton 
    function CalculateCallback(~,~) 

     % Get the values in the edit boxes. There is no ckechup to make 
     % sure the user entered a correct value... 
     A = str2double(get(hEditA,'String')); 
     B = str2double(get(hEditB,'String')); 
     C = str2double(get(hEditC,'String')); 

     Calculation = A*B+C; 

     % Display the result. 
     set(hTestResult,'String',sprintf('The result is %0.2f',Calculation));   

     % Find how many calculations have been performed and append the 
     % parameters to the current list 

     [x,~] = find(~isnan(CalculationList)); 
     CurrentCalc = numel(unique(x)); % Get number of rows which are NOT NaNs. 

     CurrentValues = [A B C]; 


     CalculationList(CurrentCalc+1,:) = CurrentValues; 
     CurrentString = sprintf('A = %0.2f B = %0.2f C = %0.2f',A,B,C); 

     % Assign the parameters to the cell array. 
     CalculationStrings(CurrentCalc+1) = {CurrentString}; 
     set(hListCalcu,'String',CalculationStrings) 

    end 

% Listbox callback: When the selection changes, the corresponding 
% parameters in the edit boxes change. 

    function ListBox_Callback(~,~) 

     SelectedCalc = get(hListCalcu,'Value'); 

     CalculationList(SelectedCalc,1) 
     CalculationList(SelectedCalc,2) 
     CalculationList(SelectedCalc,3) 

     set(hEditA,'String',CalculationList(SelectedCalc,1)); 
     set(hEditB,'String',CalculationList(SelectedCalc,2)); 
     set(hEditC,'String',CalculationList(SelectedCalc,3)); 
    end 

end 

实际的接口看起来是这样的:

enter image description here

当然,你可以把它要复杂得多,但是这应该可以帮助您开始使用并了解不同的回调如何协同工作。玩的开心!

+0

非常感谢!我会试试看看我是否能够正常工作=) – Robin 2014-10-07 06:29:53

+0

好的,我会等待您的反馈! – 2014-10-07 12:15:44

+0

它是如此工作? – 2014-10-09 13:27:51

相关问题