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!