Why does my cell array remain empty?

조회 수: 3 (최근 30일)
Kevin Gjoni
Kevin Gjoni 2023년 1월 28일
댓글: Kevin Gjoni 2023년 1월 28일
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.
Kevin Gjoni
Kevin Gjoni 2023년 1월 28일
will fix now, thank you

댓글을 달려면 로그인하십시오.

추가 답변 (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