Dealing with multiline text
446 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
easily confused
el 15 de Nov. de 2011
Comentada: Steven Lord
el 9 de Mayo de 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 comentarios
Respuesta aceptada
Fangjun Jiang
el 15 de Nov. de 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 comentarios
Fangjun Jiang
el 9 de Mayo de 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
el 9 de Mayo de 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'})
Más respuestas (4)
Walter Roberson
el 15 de Nov. de 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 comentarios
KUNHUAN
el 13 de Feb. de 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1294745/image.png)
Put the mouse cursor before "Matlab".
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1294750/image.png)
Press ENTER. Notice the line was automatically broken down.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1294755/image.png)
Add \n to the end of the last line to actually break it. Run the cell again. MAGIC!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1294760/image.png)
Put more variables in placeholders across lines if you want.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1294765/image.png)
More lines if you want.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1294770/image.png)
Put all the lines into arrays if you want (I think this addresses your needs! ).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1294775/image.png)
0 comentarios
Benjamin Davis
el 16 de Dic. de 2019
Editada: Benjamin Davis
el 16 de Dic. de 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 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!