Dealing with multiline text

조회 수: 501 (최근 30일)
easily confused
easily confused 2011년 11월 15일
댓글: Steven Lord 2023년 5월 9일
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.
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 11월 17일
You can vote for them; that helps.

댓글을 달려면 로그인하십시오.


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