Values of selected item in list box moved into a struct

2 views (last 30 days)
i have created an empty struct, how do i move the values of the selections made in a list box into a struct. Usually this is created in matrix in matlab when you run the code in the command window, but for app designer is seems different.
I have attached a the code generated from App designer for one of my list box.
How do i make the current selection or what even selection made appear in an already created struct. i have also include the code for the empty struct.
properties (Access = public)
myStruct = struct()
% Create ListBox
app.ListBox = uilistbox(app.AgeTab);
app.ListBox.Items = {'18-28', '29-39', '40-50', '51-61', '62-72', '73-83'};
app.ListBox.ValueChangedFcn = createCallbackFcn(app, @ListBoxValueChanged, true);
app.ListBox.FontName = 'Arial Black';
app.ListBox.FontSize = 24;
app.ListBox.FontColor = [0.3922 0.8314 0.0745];
app.ListBox.BackgroundColor = [0.502 0.502 0.502];
app.ListBox.Position = [40 350 795 169];
app.ListBox.Value = '18-28';
% Create GenderTab
app.GenderTab = uitab(app.TabGroup);
app.GenderTab.Title = 'Gender';
app.GenderTab.BackgroundColor = [0.502 0.502 0.502];

Accepted Answer

Voss on 22 Nov 2022
The code above creates and initializes a couple of components (a uilistbox and a uitab). Those components are stored in the app object, and anything you need to know about those components, you can get at any time.
For instance, the expression
returns the Value of app.ListBox at the time the command is run.
Generally, there is no need to create a separate data structure mirroring the state of a GUI; you can simply get the state of the GUI at any time.
What are you trying to accomplish by making a separate struct that mirrors the GUI state (if that is in fact the intention)?
(Having said that, assuming you have a good reason to do this, you can keep your struct up to date by updating it in the callbacks of your components.)
Nwasinachi on 29 Nov 2022
Edited: Nwasinachi on 29 Nov 2022
@Voss i set wordarray as a variable in the property
properties (Access = public)
wordarray % Description
Changed it to app.wordarray everywhere that i need it.
%attempt for text area Question 1
firstopt = 'The boy went to school'; % will change text and number of the
Secoption = 'The man went to church';
thoption = 'The girl is going there';
app.wordarray = {firstopt Secoption thoption};
app.TextArea7_2.Value = app.wordarray{randi(numel(app.wordarray))}; %% randomization works.
But this line is still giving me an error. i have used app.wordarray{firstopt} which it stated that it wasnt a defined variable. Do i need to add that variable into the properties as well?
if app.TextArea7_2.Value==app.wordarray{1} && app.ListBox2.Value =='YES';
Correct = Correct+1;% a loop where if this option is displayed and YES is selected from list box then the response is correct
else Incorrect = Incorrect+1;

Sign in to comment.

More Answers (0)


Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by