Why does my cell array remain empty?

3 次查看(过去 30 天)
I'm trying to find all checkboxes in a specific panel that are checked, get their tags/names and perform different actions depending on how many of them are checked.
I tried it like this but the cell array remains empty (cell array in question is "selected_names"):
cla(app.axFeatures);
% Find all the checkbox objects belonging to features
checkboxes = findall(app.features,'type','uicheckbox');
% Initialize an empty cell array to store the names of the selected checkboxes
selected_names = {};
% Loop through the checkboxes and check which ones are selected
for i=1:numel(checkboxes)
if get(checkboxes(i), 'Value') == 1
% If the checkbox is selected, add its name to the cell array
selected_names{end+1} = get(checkboxes(i), 'Tag');
end
end
if numel(selected_names) == 3
app.analyzer.histograms_3D(selected_names{1}, selected_names{2}, selected_names{3}, app.axFeatures);
elseif numel(selected_names) == 2
app.analyzer.histograms_gaussians(selected_names{1}, selected_names{2}, app.axFeatures);
elseif numel(selected_names) == 1
app.analyzer.histogram(selected_names{1}, app.axFeatures)
else
% Display error prompt for no selected checkboxes or more than 3
errordlg("Please select between 1 and 3 checkboxes.", "Invalid Selection");
end
end
How do I fix this?
  7 个评论
Kevin Gjoni
Kevin Gjoni 2023-1-28
yes, this returns the checkboxes in my specified panel
Walter Roberson
Walter Roberson 2023-1-28
What shows up for
[checkboxes.Value]
? in particular are there any nonzero values?
Does the code contain any pause() or uiwait() or waitfor() or drawnow() between the point at which the user clicks and the time this function executes?

请先登录,再进行评论。

采纳的回答

dpb
dpb 2023-1-28
移动:dpb 2023-1-28
We can't debug what we don't have, which is a sample app that recreates the problem...looks like all should work just fine here although it can be made more efficient...
fig = uifigure;
pnl = uipanel(fig);
hcbx = uicheckbox(pnl);
hcbx(2)=uicheckbox(pnl);
hcbx(2).Position=hcbx(1).Position+[0 -30 0 0];
set(hcbx,{'Tag'},cellstr("CBox"+[1:2].'))
hcbx(2).Value=true;
% now query them...
names=get(hcbx,'Tag'); % the assigned tag values
state=cell2mat(get(hcbx,'Value')); % array of state values (logicals)
selected_names=names(state) % and the checked ones only
produces
>>
...above code...
selected_names =
1×1 cell array
{'CBox2'}
>>
here. I didn't wrap it inside an app, but the logic seems sound; something must be going wrong elsewhere that the Tag values are getting trashed.
  5 个评论
dpb
dpb 2023-1-28
No problem, although if had taken suggested code at face value to begin with... :)
NOTA BENE:
checkboxes = findall(app.features,'type','uicheckbox');
names=get(checkboxes,'Tag');
Both of these could/should be determined as part of the startup code and held as global parameters for the application; there's no sense in searching for the same thing every time when it is fixed application data.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by