Nwasinachi Ezeji
Nwasinachi Ezeji

Reputation: 1

MATLAB App designer Listbox saving values

I am trying to save the User input in app designer(MATLAB). I have created an empty struct, but I am unable to figure out where the items in the Listbox are being saved. With Matlab, a matrix is usually created and one can make certain UI components persistent so that, but that doesnt seem to be the case for App designer. I have attached a copy of the code, this isnt the complete code but rather the area for the listbox

properties (Access = public)
    myStruct = struct()
end

% Callbacks that handle component events
methods (Access = private)

    % Callback function: NEXTButton_4, WelcomeTab
    function ButtonPushed(app, event)
     
        app.TabGroup.SelectedTab = app.AgeTab
       
    end

    % Value changed function: ListBox
    function ListBoxValueChanged(app, event)
        value = app.ListBox.Value;
        if strcmp(app.ListBox.Value ,'18-28')||strcmp(app.ListBox.Value ,'29-39')||strcmp(app.ListBox.Value,'40-50')||strcmp(app.ListBox.Value,'51-61')...
                ||strcmp(app.ListBox.Value,'62-72');
          set(app.NEXTButton,'Enable','on');
           Age = app.myStruct.Age;

        end
                   Age = app.ListBox.Value;
%             save('Age.mat',"Age")
          %  save('Dummyfile.mat', '-struct', myStruct)
          
%             for i = 1:numel(app.ListBox.Items)
%     index(i) = isequal(app.ListBox.Value{1}, [app.ListBox.ItemsData{i}]);
%             end
%             idx = find(index); % Find indice of nonzero element
% ItemName = app.ListBox.Items{idx}

i have tried to create an index, but it didn't work.

Upvotes: 0

Views: 420

Answers (1)

il_raffa
il_raffa

Reputation: 5190

I'm not sure I understand the question correctly, but, if you want to save the values of all selected items you can:

Add two properties to the app:

  • a cellarray in which to store the value of the selected items; if you need to save only it, you don't need a struct
  • a counter to be used as index for the cellarray
properties (Access = public)
   Selected_items = {} % Description
   n_ages;
end

In the startupFcn:

  • Initialize the cellarray to empty
  • Set the listbox value to an empty array to specify non selection at the beginning (othewise, the first item will appear as selected by default when the app is created and the callback ListBoxValueChanged will not catch the selection of it)
  • Iniitalize the counter to 0
function startupFcn(app)
   app.Selected_items = {};
   app.ListBox.Value = {};
   app.n_ages = 0;
end

In the ListBoxValueChanged callback:

  • Get the value od the selected item
  • Increment the counter
  • Store the the value in the app cellarray you have added; in this way the list of selected items will be available to other callbacks and functions of your app without the need to store them in a ".mat file"
  • Copy the content of the cellarray
  • Save the cellarray in a ".mat" file
   function ListBoxValueChanged(app, event)
      value = app.ListBox.Value;
      app.n_ages = app.n_ages + 1;
      app.Selected_items{app.n_ages} = value;
      Selected_Items=app.Selected_items;
      save('SEL_ITEMS.mat','Selected_Items');
   end

Note: you can easily adapt the code of the ListBoxValueChanged callback above to take into acount the if conditions

Upvotes: 0

Related Questions