Search code examples
stringmatlabline-breaks

Line breaks in MATLAB strings


I am writing a code in which I am asking the user for an input. However the string informing the user about this is somewhat long, and when I use the code, it all gets written on a single line in the command window. I would like to have this spread over multiple lines. My code is:

n = input(['The matrix is diagonally dominant.  Please choose which method you wish to'...
        ' use: 1 (Gaussian elimination), 2 (Jacobi iterations),'...
        ' 3 (Gauss-Seidel iterations).  If you enter any other number'...
        ' Gaussian elimination will automatically be used: ']);

If preferable, I would like to have this displayed over 4 lines, as in the code. How can I go about getting this done?


Solution

  • use sprinf and \n (newline character)

    n = input(sprintf(['The matrix is diagonally dominant.  Please choose which method you wish to\n'...
        ' use: 1 (Gaussian elimination), 2 (Jacobi iterations),\n'...
        ' 3 (Gauss-Seidel iterations).  If you enter any other number\n'...
        ' Gaussian elimination will automatically be used: ']));