Nested for loop still not working...

Why will this work...
if true
% code
end
InstNames = fieldnames(HistData);
HistData.(InstNames{1}).(DataNames{1}) = SepHistData(:,1,1);
HistData.(InstNames{1}).(DataNames{2}) = SepHistData(:,2,1);
HistData.(InstNames{1}).(DataNames{3}) = SepHistData(:,3,1);
HistData.(InstNames{1}).(DataNames{4}) = SepHistData(:,4,1);
HistData.(InstNames{1}).(DataNames{5}) = SepHistData(:,5,1);
HistData.(InstNames{1}).(DataNames{6}) = SepHistData(:,6,1);
HistData.(InstNames{1}).(DataNames{7}) = SepHistData(:,7,1);
HistData.(InstNames{2}).(DataNames{1}) = SepHistData(:,1,2);
HistData.(InstNames{2}).(DataNames{2}) = SepHistData(:,2,2);
HistData.(InstNames{2}).(DataNames{3}) = SepHistData(:,3,2);
HistData.(InstNames{2}).(DataNames{4}) = SepHistData(:,4,2);
HistData.(InstNames{2}).(DataNames{5}) = SepHistData(:,5,2);
HistData.(InstNames{2}).(DataNames{6}) = SepHistData(:,6,2);
HistData.(InstNames{2}).(DataNames{7}) = SepHistData(:,7,2);
and this won't...
if true
% code
end
for k = length(InstNames)
for i = length(DataNames)
HistData.(InstNames{k}).(DataNames{i}) = SepHistData(:,i,k);
end
end

 Akzeptierte Antwort

Paul
Paul am 18 Feb. 2014
Bearbeitet: Paul am 18 Feb. 2014

0 Stimmen

You should do
for k = 1:length(InstNames)
for i = 1:length(DataNames)
So add the 1:. This:
for k = length(InstNames)
assigns the value length(InstNames) to k without looping.

1 Kommentar

Justin
Justin am 19 Feb. 2014
Thanks so much. These are the moments when one is reminded he's a beginner (I feel like exclaiming, DUH!).
Thanks, again.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sean de Wolski
Sean de Wolski am 18 Feb. 2014

0 Stimmen

length(DataNames)
Will return something like seven I imagine.
Your for-loops will eventually want
SepHistData(7,7)
Which doesn't exist. This is not one of the options above
It seems to me like you want:
for k = 1:2
for i = 1:length(DataNames)
etc.

1 Kommentar

Sorry, that was a bad example, this is what I am really working with.
if true
% code
end
for k = length(InstNames)
for i = length(DataNames)
HistData.(InstNames{k}).(DataNames{i}) = SepHistData(:,i,k);
end
end
clearvars i k

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 18 Feb. 2014

Kommentiert:

am 19 Feb. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by