Dealing with multiline text
489 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
easily confused
le 15 Nov 2011
Commenté : Steven Lord
le 9 Mai 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
0 commentaires
Réponse acceptée
Fangjun Jiang
le 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 commentaires
Fangjun Jiang
le 9 Mai 2023
newline() Introduced in R2016b? LOL. It's the same as char(10)
double(newline)
Another way is to use string array but it's similar to cell array of strings.
Steven Lord
le 9 Mai 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
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"]
or write a multi-line title in a plot:
title({'abracadabra';'hocus pocus'})
Plus de réponses (4)
Walter Roberson
le 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.
0 commentaires
KUNHUAN
le 13 Fév 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! ).
0 commentaires
Benjamin Davis
le 16 Déc 2019
Modifié(e) : Benjamin Davis
le 16 Déc 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);
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!