2014-10-06 80 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

相關問題