Values of selected item in list box moved into a struct
3 次查看(过去 30 天)
显示 更早的评论
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];
0 个评论
采纳的回答
Voss
2022-11-22
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 个评论
Voss
2022-11-29
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!