Values of selected item in list box moved into a struct

3 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()
end
% 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
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
app.ListBox.Value
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.)
  15 Comments
Voss
Voss on 29 Nov 2022
Each function has its own workspace, so ListBox2ValueChanged doesn't know about wordarray because wordarray is defined inside another function.
In an app, you can make a variable an app property and define it in the startup function so that it can be seen by all functions in the app. You can do that for wordarray by including this in your startup function:
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};
and refer to app.wordarray instead of wordarray everywhere.
Nwasinachi
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
end
Changed it to app.wordarray everywhere that i need it.
%attempt for text area Question 1
Correct=0;
Incorrect=0;
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;
end

Sign in to comment.

More Answers (0)

Categories

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!