Search code examples
scilab

How to print a complex number without percent sign in Scilab?


I tried this

a = 1+3*%i;
disp("a = "+string(a))

I got a = 1+%i*3 , but what I want is a = 1. + 3.i

So is there any method in Scilab to print a complex number without the percent sign?


Solution

  • Similarly to Matlab, you can format the output string by including the real and imaginary parts separately.

    mprintf('%g + %gi\n', real(a) , imag(a))
    

    However, that looks pretty ugly when the imaginary part is negative. I suggest writing a formatting function:

    function s = complexstring(a)
        if  imag(a)>=0 then
            s = sprintf('%g+%gi', real(a) , imag(a))
        else 
            s = sprintf('%g%gi', real(a) , imag(a))
        end
    endfunction
    

    Examples:

    disp('a = '+complexstring(1+3*%i))
    disp('b = '+complexstring(1-3*%i))
    

    Output:

    a = 1+3i
    b = 1-3i