From: Nives Fakin on
a function i wrote requires defining the variable n, but if i define it in the begining of the function, since it is a recursion, it changes it to the old value every time. how to avoid it? please don't resent if it is a silly question, i'm a beginner with matlab.

this is the critical loop inside the function "identification"

for i=1:k
if A(i)>thr
eval(['C_' num2str(n) '=C(1:br1,1:br1)']);
eval(['C_' num2str(n+1) '=C(br1+1:k,br1+1:k)']);
n=n+2;
clear B;
counter=counter+1;
identification(C(1:br1,1:br1));
identification(C(br1+1:k,br1+1:k));
break
elseif i<k
continue
end
end
From: Jan Simon on
Dear Nives,

> for i=1:k
> if A(i)>thr
> eval(['C_' num2str(n) '=C(1:br1,1:br1)']);
> eval(['C_' num2str(n+1) '=C(br1+1:k,br1+1:k)']);
> n=n+2;
> clear B;
> counter=counter+1;
> identification(C(1:br1,1:br1));
> identification(C(br1+1:k,br1+1:k));
> break
> elseif i<k
> continue
> end
> end

I do not understand the problem with the variable n. Could you post a minimal example, which reproduces your problem?

I'd really avoid using EVAL. If you really really need to encode some information about the inner strukture in the names of the variables, use dynamic field names:
C.(sprintf('field%d', n)) = C(...)
But in general it is not recommended to mix structure information with names. It is comparable to name children with their birthdays and tax number - although it might be unique or helpful sometimes.
At least the EVAL method slows down Matlab brutally, but increases the complexity of debugging dramatically.

> elseif i<k
> continue
Does exactly nothing, so omit it.
The "clear B" is confusing here. Are you sure it is needed??

Kind regards, Jan