Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline string literal in Matlab?

Is there a multiline string literal syntax in Matlab or is it necessary to concatenate multiple lines?

I found the verbatim package, but it only works in an m-file or function and not interactively within editor cells.

EDIT: I am particularly after readbility and ease of modifying the literal in the code (imagine it contains indented blocks of different levels) - it is easy to make multiline strings, but I am looking for the most convenient sytax for doing that.

So far I have

t = {...
'abc'...
'def'};
t = cellfun(@(x) [x sprintf('\n')],t,'Unif',false);
t = horzcat(t{:});

which gives size(t) = 1 8, but is obviously a bit of a mess.

EDIT 2: Basically verbatim does what I want except it doesn't work in Editor cells, but maybe my best bet is to update it so it does. I think it should be possible to get current open file and cursor position from the java interface to the Editor. The problem would be if there were multiple verbatim calls in the same cell how would you distinguish between them.

like image 478
robince Avatar asked Jan 09 '13 11:01

robince


People also ask

What does [] mean in Matlab?

Description: Square brackets enable array construction and concatenation, creation of empty matrices, deletion of array elements, and capturing values returned by a function.

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.


2 Answers

I'd go for:

multiline = sprintf([ ... 
'Line 1\n'... 
'Line 2\n'... 
]);
like image 198
edgar.holleis Avatar answered Sep 18 '22 03:09

edgar.holleis


Matlab is an oddball in that escape processing in strings is a function of the printf family of functions instead of the string literal syntax. And no multiline literals. Oh well.

I've ended up doing two things. First, make CR() and LF() functions that just return processed \r and \n respectively, so you can use them as pseudo-literals in your code. I prefer doing this way rather than sending entire strings through sprintf(), because there might be other backslashes in there you didn't want processed as escape sequences (e.g. if some of your strings came from function arguments or input read from elsewhere).

function out = CR()
out = char(13); % # sprintf('\r')

function out = LF()
out = char(10); % # sprintf('\n');

Second, make a join(glue, strs) function that works like Perl's join or the cellfun/horzcat code in your example, but without the final trailing separator.

function out = join(glue, strs)
strs = strs(:)';
strs(2,:) = {glue};
strs = strs(:)';
strs(end) = [];
out = cat(2, strs{:});

And then use it with cell literals like you do.

str = join(LF, {
    'abc'
    'defghi'
    'jklm'
    });

You don't need the "..." ellipses in cell literals like this; omitting them does a vertical vector construction, and it's fine if the rows have different lengths of char strings because they're each getting stuck inside a cell. That alone should save you some typing.

like image 35
Andrew Janke Avatar answered Sep 19 '22 03:09

Andrew Janke