Why does my cell array remain empty?
4 次查看(过去 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 个评论
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
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
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 Center 和 File Exchange 中查找有关 Install Products 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!