Nested Functions

Altair Forum User
Altair Forum User
Altair Employee
edited October 2020 in Community Q&A

Hello,

 

I am trying to convert a function I created in Matlab 2016a to Compose 2017.3. This function has nested functions inside and I am receiving errors in Compose. In order to better explain the issue, I created a sample code. Please see it below.

 

function [out1 outN]=bb(in1)
    in2=in1*3;
    out2=cc(in2);
    out1=out2;
    outN=ee(in1);
    
    
    function out2=cc(in2)
        in3=in2*4;
        out3=dd(in3);
        out2=out3;
        
        function out3=dd(in3)
            out3=in3^2;
        end
    end
    
    function outN=ee(in1)
        outN=in1^3;
    end
    
end

 

 

When I save this code as bb.oml, and run [A B]=bb(2), I receive the following error message:

Unknown function: cc in function bb at line number 3 in file bb.oml

 

Is there a way to make nested functions works in Compose?

 

Thank you

Berker

 

Tagged:

Answers

  • manoj kandukuri
    manoj kandukuri
    Altair Employee
    edited February 2021

    Hi Berker,

    can you try sample 1 below. 'end' statement for function 'function [out1 outN]=bb(in1)' is at the last line of your sample script, causing the function 'cc' 'dd' 'ee'  not been evaluated in line 3.  If you want to have functions 'cc','dd','ee' to be nested inside the function'bb' then try sample 2 below. Hope this helps.

     

    sample 1. 

    function [out1 outN]=bb(in1)
        in2=in1*3;
      out2=cc(in2);
     out1=out2;
        outN=ee(in1);
    end
      % end the function bb
      function out2=cc(in2)
            in3=in2*4;
            out3=dd(in3);
            out2=out3;
            
            function out3=dd(in3)
                out3=in3^2;
            end
        end

        
        function outN=ee(in1)
            outN=in1^3;
        end

     

    Sample2:

    function [out1 outN]=bb(in1)
        in2=in1*3;

      function out2=cc(in2)
            in3=in2*4;
            out3=dd(in3);
            out2=out3;
            
            function out3=dd(in3)
                out3=in3^2;
            end
       
        end
        
        function outN=ee(in1)
            outN=in1^3;
        end

    % evaluating  function calls 'cc' 'ee' 
        out2=cc(in2);
        out1=out2;
        outN=ee(in1);
    end

     

    Thanks,

    Manoj