Structures with for loop

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 comentarios

Geoff Hayes
Geoff Hayes el 31 de Jul. de 2016
Ozge - how is the sample field initialized? If it has been set to be an array of doubles, then the error message makes sense. For example,
samples = zeros(1,4);
samples(1,2) = initialization(1,2,3);
generates the same error message as above.
Ozge Moral
Ozge Moral el 31 de Jul. de 2016
i just add A.sample = [], nothing changed.
Ozge Moral
Ozge Moral el 31 de Jul. de 2016
i think there is a mistake about temp1, temp2 variables.
Geoff Hayes
Geoff Hayes el 31 de Jul. de 2016
Ozge - can you post all of your code as it is difficult to run it when we don't know what A or sz are initialized to.
Ozge Moral
Ozge Moral el 31 de Jul. de 2016
You are right. A is StartingValue structure, its my initial point. I store it A, because i need to repeat process until sz is great than 0.0001. My first code is below. Thanks for your interesting.
f = @(x) sin(5.1*pi*x+0.5).^6;
StartingValue=initialization(0,1,20);
n=20;
pheromone = UpdatePhe(20);
[StartingValue,pheromone] = initial2(StartingValue,pheromone,n,f);
[StartingValue] = Moving(StartingValue, pheromone,n);
A=struct;
B=struct;
A=StartingValue;
B=pheromone;
sz=StartingValue.sz;
temp1 = struct;
temp2 = struct;
Geoff Hayes
Geoff Hayes el 31 de Jul. de 2016
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 el 31 de Jul. de 2016
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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 31 de Jul. de 2016

Comentada:

el 31 de Jul. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by