Zero out values in multiple fields of a struct
Mostra commenti meno recenti
Suppose I have
S.ABC0500 = struct('Keys',[1:9],'Values',rand(1,9));
S.ABC0800 = struct('Keys',[1:9],'Values',rand(1,9));
S.ABC0900 = struct('Keys',[1:9],'Values',rand(1,9));
S.Title = 'MyData';
S.Date = datetime('today');
How can I efficiently set all the values to zero? That is, I want to do
S.ABC0500.Values = zeros(1,9);
S.ABC0800.Values = zeros(1,9);
S.ABC0900.Values = zeros(1,9);
The number of ABC variables is large.
1 Commento
Stephen23
il 5 Mag 2023
"How can I efficiently set all the values to zero"
With more suitable data design: don't force meta-data into fieldnames. A non-scalar structure would likely be better:
Risposta accettata
Più risposte (1)
I would recommend a different data organization:
S.Title = 'MyData';
S.Date = datetime('today');
S.ID=["ABC0500", "ABC0500", "ABC0900"]';
S.Values=rand(3,9);
S.Keys=repmat(1:9,3,1);
and then you can simply do,
S.Values(:)=0;
Categorie
Scopri di più su C2000 Microcontroller Blockset in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!