Remove programmatically added checkboxes and replace them with new checkboxes

18 ビュー (過去 30 日間)
Stephen
Stephen 2022 年 5 月 4 日
編集済み: Stephen 2022 年 5 月 5 日
I am working on an app that dynamically adds checkboxes based on the data loaded to a panel. I want to replace the checkboxes with new checkboxes with different names depending on the file I load.
The data loaded here are the names of the files in a given folder to populate a set of checkboxes, whose number can vary.
How do I then remove the checkboxes when I load a different set of files?
names=dir('*train*.mat');% identify files with 'train' in name
r={names(:).name}.'; % Create an cell structure of names
StrErase=["Train","Validation","train","validation","ing"]; % erase parts of the string
% Load Posture data and names
for i=1:length(r)
str=replace(erase(r(i),".mat"),'_',' '); % remove the file extension from the name
app.Postcbx{i}=uicheckbox('Parent',app.PosturesPanel,"Value",1,'Position',...
[10 (330-20*i) 150 15],'Text',erase(str,StrErase)); % Create the checkboxes
end
  3 件のコメント
Stephen
Stephen 2022 年 5 月 4 日
I want to remove the checkboxes when I load a new set of files.
Jan
Jan 2022 年 5 月 4 日
What about:
delete(app.Postcbx)
or if they are really defined as cell elements:
delete([app.Postcbx{:}])

サインインしてコメントする。

採用された回答

Voss
Voss 2022 年 5 月 4 日
編集済み: Voss 2022 年 5 月 5 日
names=dir('*train*.mat');% identify files with 'train' in name
r={names(:).name}.'; % Create an cell structure of names
StrErase=["Train","Validation","train","validation","ing"]; % erase parts of the string
n_old = numel(app.Postcbx);
n_new = numel(r);
if n_old > n_new
% delete any old checkboxes you don't need
delete([app.Postcbx{n_new+1:n_old}]);
app.Postcbx(n_new+1:n_old) = [];
elseif n_old < n_new
% create any new checkboxes you need
for i = n_old+1:n_new
app.Postcbx{i}=uicheckbox('Parent',app.PosturesPanel);
end
end
% update the Position, Text, and Value of all checkboxes
for i = 1:n_new
str=replace(erase(r(i),".mat"),'_',' '); % remove the file extension from the name
app.Postcbx{i}.Position = [10 (330-20*i) 150 15];
app.Postcbx{i}.Text = erase(str,StrErase);
app.Postcbx{i}.Value = 1;
end
  3 件のコメント
Voss
Voss 2022 年 5 月 5 日
編集済み: Voss 2022 年 5 月 5 日
Perhaps those checkboxes that persisted were not stored in app.Postcbx? Hard to say without seeing more of the app code.
In general, it should be feasible to delete/create objects as needed, without having to delete all and then create a new set each time the set changes.
EDIT: Actually, I just made a modification to my answer. Try it like it is now, and see if that works, if you're still planning to use checkboxes as opposed to some other type of component.
Stephen
Stephen 2022 年 5 月 5 日
編集済み: Stephen 2022 年 5 月 5 日
I wasn't able to re-write the text on the checkboxes using this method. I ended up using this
if numel(app.Postcbx)>0
delete([app.Postcbx{:}]);
end
and making new checkboxes each time I open a new data folder.
Thanks!

サインインしてコメントする。

その他の回答 (2 件)

Steven Lord
Steven Lord 2022 年 5 月 4 日
Are you sure you want to use an unknown number of checkboxes? If there are a large number of files perhaps a CheckBoxTree (uitree) or a ListBox (uilistbox) might be a better solution.

Stephen
Stephen 2022 年 5 月 5 日
This seems the best solution, thanks!

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by