Robin
Robin

Reputation: 1

Save multiple GUIdata sets to listbox and then load them again in MATLAB

I have built a calculation software in MATLAB GUIDE. What I want to do is to fill out all my calculation data in different edit fields and some dropdowns and when I press calculate a "listbox" should be populated with the text "Calculation 1".

If I then change some data in some of the input fields and press calculate again I want to populate the listbox with "Calculation 2" beneath "Calculation 1" etc...

But then I would want to be able to highligt "calculation 1" again in the listbox and press a "load input parameters" button to populate all the edit input fields with the data that was used when "calculation 1" was calculated.

I have looked all over the place for this but can't find anything.

//Robin

Upvotes: 0

Views: 453

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

Here is some code which is very basic but performs what you are looking for. There are a lot of tweaks possible but I'll let you play around with them. I put explanations as comments. You can copy past into Matlab and change the GUI as you like.

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

The actual interface looks like this:

enter image description here

Of course you can make it much more complex, but this should help you get started and understand how the different callbacks work together. Have fun!

Upvotes: 1

Related Questions