How to get rid of the for loop ?

1 vue (au cours des 30 derniers jours)
Robert Thiel
Robert Thiel le 9 Sep 2015
Commenté : Robert Thiel le 10 Sep 2015
the task
handles.plotdata.IR1 = zeros(1, 2001);
handles.plotdata.IR2 = zeros(1, 2001);
handles.plotdata.ax = zeros(1, 2001);
handles.plotdata.ay = zeros(1, 2001);
handles.plotdata.az = zeros(1, 2001);
my simplification so far
Kanal_name = {'IR1' 'IR2' 'ax' 'ay' 'az'};
for n = 1:length(Kanal_name)
handles.plotdata.(Kanal_name{n}) = zeros(1, 2001);
end
now i want to get rid of the for loop, any suggestions ?
  1 commentaire
James Tursa
James Tursa le 9 Sep 2015
Why do you want to get rid of the loop?

Connectez-vous pour commenter.

Réponse acceptée

Kelly Kearney
Kelly Kearney le 9 Sep 2015
Maybe this?
tmp = cell(2,length(Kanal_name));
tmp(1,:) = Kanal_name;
[tmp{2,:}] = deal(zeros(1,2001));
handles.plotdata = struct(tmp{:});
But this makes the code more difficult to read without adding any real benefit that I can see (the time difference is pretty negligible). Unless you have a definitive need to eliminate loops, I'd stick with your version.
  2 commentaires
per isakson
per isakson le 9 Sep 2015
... or this
>> clear all
>> handles.plotdata = cell2struct( repmat({zeros(1, 2001)},1,5) ...
, {'IR1' 'IR2' 'ax' 'ay' 'az'}, 2 );
>> handles.plotdata
ans =
IR1: [1x2001 double]
IR2: [1x2001 double]
ax: [1x2001 double]
ay: [1x2001 double]
az: [1x2001 double]
Robert Thiel
Robert Thiel le 10 Sep 2015
Thank you very much, both of you. Helps a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by