Dealing with multiline text

501 views (last 30 days)
easily confused
easily confused on 15 Nov 2011
Commented: Steven Lord on 9 May 2023
I know how to open a file, access the data, and parse into arrays with regular expressions. What I want to do is somehow set a variable to a multiline text value and parse it in the same way. I would like to be able to do the following
x='line1 x 789
line2; y 483
line3{}
line4 1 4 7 9'
and then be able to parse each line of x. The goal is require a minimal amount of error prone editing to data being pasted from a file which will then be parsed into arrays. I don't want to create files for each text section. Is my only choice
x(1) = ...
x(2) = ...
etc Thanks
easily_confused

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 15 Nov 2011
In fact, it is quite inconvenient to create a multiline text string in MATLAB. I've tried the first approach as below before. But more often, I use cell array of strings. It's not that hard to make the cell string into multiple lines of text
x=['line1 x 789',char(10),...
'line2 y 483',char(10),...
'line3 {}',char(10),...
'line4 1 4 7 9'];
y={'line1 x 789'
'line2 y 483'
'line3 {}'
'line4 1 4 7 9'};
str=sprintf('%s\n',y{:})
  4 Comments
Fangjun Jiang
Fangjun Jiang on 9 May 2023
newline() Introduced in R2016b? LOL. It's the same as char(10)
double(newline)
ans = 10
Another way is to use string array but it's similar to cell array of strings.
Steven Lord
Steven Lord on 9 May 2023
Yes, newline does just return char(10). But using newline avoids the magic number anti-pattern, in much the same way that calling pi instead of hard-coding:
pi
ans = 3.1416
can make the intent of code clearer.
So what exactly are you planning to do with this multi-line text? There are certain options depending on whether you want to store multiple elements of data in the form of a column:
triplets = ["Huey"; "Dewey"; "Louie"]
triplets = 3×1 string array
"Huey" "Dewey" "Louie"
or write a multi-line title in a plot:
title({'abracadabra';'hocus pocus'})

Sign in to comment.

More Answers (4)

Walter Roberson
Walter Roberson on 15 Nov 2011
MATLAB has no multi-line character string syntax. The closest it gets is
x = {
'line 1 x 789'
'line2; y 483'
'line3{}'
'line4 1 4 7 9'
};
There are possibilities such as
x = regexp('line 1 x 789|line2; y 483|line3{}|line4 1 4 7 9', '\|', 'split');
If you use char(10) (newline, sprintf('\n')) as the line break character,
x = 'line 1 x 789|line2; y 483|line3{}|line4 1 4 7 9';
x(x=='|') = char(10);
then you can textscan(x,...) and textscan will treat it as the input and will recognize the newlines.

KUNHUAN
KUNHUAN on 13 Feb 2023
A good way is to combine fprintf with [].
Adding \n and ... at the end of every line.
Press ENTER can automatically break the lines from the mouse cursor.
Make up spaces for any variables with the % sign.
Supporting array storage.
Example:
Single line text
Put the mouse cursor before "Matlab".
Press ENTER. Notice the line was automatically broken down.
Add \n to the end of the last line to actually break it. Run the cell again. MAGIC!
Put more variables in placeholders across lines if you want.
More lines if you want.
Put all the lines into arrays if you want (I think this addresses your needs! ).

easily confused
easily confused on 16 Nov 2011
how do I accept both answers? Both are good, thanks for the help.

Benjamin Davis
Benjamin Davis on 16 Dec 2019
Edited: Benjamin Davis on 16 Dec 2019
I created a File Exchange submission to address this exact issue. It brings heredoc/herestring syntax to MATLAB by parsing specially formatted comments.
For example:
%Source: https://json.org/example.html
%{
json << END
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
END
%}
%this call parses the above comment
hd = heredoc();
%the heredoc is made available under the fieldname 'json'
obj = jsondecode(hd.json);

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!