Why does my cell array remain empty?
Ältere Kommentare anzeigen
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 Kommentare
Chris
am 28 Jan. 2023
Have you assigned tags to your checkboxes?
Kevin Gjoni
am 28 Jan. 2023
dpb
am 28 Jan. 2023
Because unless your app sets it, the 'Tag' property is empty...see the doc for 'CheckBox Properties' that says for the Tag property--
Tag — Object identifier
'' (default) | character vector | string scalar
Object identifier, specified as a character vector or string scalar. You can specify
a unique Tag value to serve as an identifier for an object. When you need access to
the object elsewhere in your code, you can use the findobj function to search for the
object based on the Tag value.
BUT, in order to do that, the property value has to be set to begin with. If you just want an identifier for each, then set the property in the design mode.
Kevin Gjoni
am 28 Jan. 2023
Walter Roberson
am 28 Jan. 2023
checkboxes = findall(app.features,'type','uicheckbox');
is that returning a nonempty result?
Kevin Gjoni
am 28 Jan. 2023
Walter Roberson
am 28 Jan. 2023
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?
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!