Dealing with multiline text

498 次查看(过去 30 天)
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

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-11-15
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 个评论
Fangjun Jiang
Fangjun Jiang 2023-5-9
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 2023-5-9
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'})

请先登录,再进行评论。

更多回答(4 个)

Walter Roberson
Walter Roberson 2011-11-15
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 2023-2-13
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 2011-11-16
how do I accept both answers? Both are good, thanks for the help.

Benjamin Davis
Benjamin Davis 2019-12-16
编辑:Benjamin Davis 2019-12-16
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);

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by