Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 821
Kristian Avatar asked Sep 27 '12 11:09

Kristian


People also ask

How do you add a line break in MATLAB?

c = newline creates a newline character. newline is equivalent to char(10) or sprintf('\n') . Use newline to concatenate a newline character onto a character vector or a string, or to split text on newline characters.

How do you break apart a string in MATLAB?

newStr = split( str , delimiter ) divides each element of str at the delimiters specified by delimiter . The output newStr does not include the delimiters. newStr = split( str , delimiter , dim ) divides each element of str into a vector oriented along the dimension specified by dim .

How do you split a string into multiple lines in MATLAB?

newStr = splitlines( str ) splits str at newline characters and returns the result as the output array newStr . splitlines splits at actual newline characters, not at the literal \n . To split a string that contains \n , first use compose and then use splitlines .

How do I split a string into a new line?

To split a string by newline, call the split() method passing it the following regular expression as parameter - /\r?\ n/ . The split method will split the string on each occurrence of a newline character and return an array containing the substrings.


1 Answers

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: ']));
like image 114
angainor Avatar answered Oct 26 '22 04:10

angainor