Structures with for loop

11 次查看(过去 30 天)
Ozge Moral
Ozge Moral 2016-7-31
评论: Ozge Moral 2016-7-31
I have a problem like
"Conversion to double from struct is not possible.
Error in calisma7 (line 9)
A(1,j).sample(1,i)=initialization(new_a,new_b,10);"
My code is below. Code creates structures in structure until condition satisfies. But,at second iteration,it gives above error. When i tried to make A(1,2).sample(1,1) = initial... , it gave correct answer, but it didn't in loop.
while sz>0.0001
for j=1:length(A)
for i=1:length(A(1,j).AntObserved)
p = A(1,j).AntObserved(i);
d = A(1,j).interval{p};
new_a =min(d);
new_b =max(d);
A(1,j).sample(1,i)=initialization(new_a,new_b,10);
B(1,j).sample(1,i)=UpdatePhe(10);
[A(1,j).sample(1,i),B(1,j).sample(1,i)]=initial2(A(1,j).sample(1,i),B(1,j).sample(1,i),10,f);
A(1,j).sample(1,i)=Moving(A(1,j).sample(1,i),B(1,j).sample(1,i),10);
temp1 = A.sample;
temp2 = B.sample;
end
end
A=temp1;
B=temp2;
sz=A.sz;
end
I couldn't find what problem is. About structure field name? In addition, i should add initialization function:
function [ StartingValue] = initialization( a,b,n )
% [a,b] is interval of function that minimum points will search.
% n is pre-assigned number, # of subinterval.
% Intervals and midpoint of these intervals are described.
% a=a;
% b=b;
% n=n;
sz=(b-a)/n;
StartingValue.sz = sz;
StartingValue.interval = {};
StartingValue.x ={};
StartingValue.fn =[];
StartingValue.eta =[];
StartingValue.allowed = [];
StartingValue.Ants = ones(n,1);
StartingValue.AntObserved = {};
for i=1:n
StartingValue.interval{i} = [a+(i-1)*sz a+i*sz];
StartingValue.x{i} = a+(i-0.5)*sz;
StartingValue.fn(i) =0;
end
end
  7 个评论
Geoff Hayes
Geoff Hayes 2016-7-31
Ozge - you need to provide a working sample of your code that can easily demonstrate the problem. What is your UpdatePhe or initial2 methods? How do they relate to the code that you posted in your question? I also suggest using the MATLAB debugger to step through your code.
Ozge Moral
Ozge Moral 2016-7-31
A stores 1x6 sample. These 6 sample structure each stores samples.(ex:A.sample.sample.sample) But i can't explain exactly. Thanks for your interest. I will try to find another way.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by