function [tempVars subExpr]=decomp_expr(expr)

str = findsym(sym(expr));
eval(['vars={''' strrep(str,', ',''', ''') '''};']);

subExpr={};
tempVars={};
%tempVars{length(tempVars)+1}=genvarname('temp', [vars, tempVars])
%tempVars{end}


parLevel = par_level(expr);


clear subInd
for I=max(parLevel):-1:0
    Ii = find(parLevel == I);
    subI = burp(Ii);
    for k=1:length(subI)
        sub = expr(subI{k});
        loc = strfind(expr,sub);
        if length(loc)>1
            %for m=1:length(loc)
                tempVars{length(tempVars)+1} = genvarname('temp', [vars, tempVars]);
                subExpr{length(subExpr)+1} = expr(loc(1):loc(1)+length(sub)-1);
            %end
        end
        parLevel(subI{k})=max(0,I-1);
    end
end








function level=par_level(expr)

L=length(expr);

lev=0;

for k=1:L
    if expr(k)=='(', lev=lev+1; end
    if (k>1 & expr(k-1)==')'), lev=lev-1; end
    level(k)=lev;
end

I=find(expr=='(');

for k=1:length(I)
    io=I(k);
    n=1;
    while ((io-n)>1) & (expr(io-n)~='+' & expr(io-n)~='-' & expr(io-n)~='*' & expr(io-n)~='/' & expr(io-n)~='^' & expr(io-n)~=' ' & expr(io-n)~='(' & expr(io-n)~=')')
        level(io-n)=level(io);
        n=n+1;
    end
end




function C=burp(Ii)
C={};
v=Ii(2:end)-Ii(1:end-1);
while ~isempty(find(v==1,1,'first'))
    ind=find(v==1,1,'first');
    n=1;%n=0;
    x=Ii(ind);
    while (ind+n<=length(v))&(v(ind+n)==1)
        x=[x Ii(ind+n+1)];
        n=n+1;
    end
    C=[C {Ii(ind:ind+n)}];
    v(ind:ind+n-1)=0;
end
