wraptextFS

wraptextFS formats long strings into wrapped text of specified width.

Syntax

  • strFormatted=wraptextFS(str)example
  • strFormatted=wraptextFS(str,Name,Value)example

Description

This function not only does text wrapping, but also enables us: 1) to control left margin of the text;

2) to control the maximum width of the text or the right margin;

3) to add a (comment) sign at the beginning of each row of the wrapped text;

4) to indent the first line of the text.

5) to personalize comments, and left margin for comments.

This function uses routine strjoin and therefore can be used just by those who have a version of MATLAB>=2013a

example

strFormatted =wraptextFS(str) wraptextFS with all default options.

example

strFormatted =wraptextFS(str, Name, Value) start text in column 3 and put percentage sign at the beginning of each line.

Examples

expand all

  • wraptextFS with all default options.
  • str='Paene insularum, Sirmio, insularumque ocelle, quascumque in liquentibus stagnis marique vasto fert uterque Neptunus, quam te libenter quamque laetus inviso, vix mi ipse credens Thuniam atque Bithunos liquisse campos et videre te in tuto. o quid solutis est beatius curis, cum mens onus reponit, ac peregrino labore fessi venimus larem ad nostrum, desideratoque acquiescimus lecto? hoc est quod unum est pro laboribus tantis. salve, o venusta Sirmio, atque ero gaude gaudente, vosque, o Lydiae lacus undae, ridete quidquid est domi cachinnorum.';
    if verLessThan('matlab','8.1') ==1
    warning('This function uses routine strjoin and works just with Matlab >=2013a')
    else
    Newstr=wraptextFS(str)
    end
    Newstr =
    
        'Paene insularum, Sirmio, insularumque ocelle, quascumque in liquentibus
         stagnis marique vasto fert uterque Neptunus, quam te libenter quamque
         laetus inviso, vix mi ipse credens Thuniam atque Bithunos liquisse campos
         et videre te in tuto. o quid solutis est beatius curis, cum mens onus
         reponit, ac peregrino labore fessi venimus larem ad nostrum, desideratoque
         acquiescimus lecto? hoc est quod unum est pro laboribus tantis. salve, o
         venusta Sirmio, atque ero gaude gaudente, vosque, o Lydiae lacus undae,
         ridete quidquid est domi cachinnorum.
         '
    
    

  • start text in column 3 and put percentage sign at the beginning of each line.
  • str='Paene insularum, Sirmio, insularumque ocelle, quascumque in liquentibus stagnis marique vasto fert uterque Neptunus, quam te libenter quamque laetus inviso, vix mi ipse credens Thuniam atque Bithunos liquisse campos et videre te in tuto. o quid solutis est beatius curis, cum mens onus reponit, ac peregrino labore fessi venimus larem ad nostrum, desideratoque acquiescimus lecto? hoc est quod unum est pro laboribus tantis. salve, o venusta Sirmio, atque ero gaude gaudente, vosque, o Lydiae lacus undae, ridete quidquid est domi cachinnorum.';
    if verLessThan('matlab','8.1') ==1
    warning('This function uses routine strjoin and works just with Matlab >=2013a')
    else
    Newstr=wraptextFS(str,'comment',true,'startcolumn',3)
    end

    Related Examples

    expand all

  • Example of specification of startcolumn and text width.
  • Start text in column 5, the maximum text width is 40.

    str='Paene insularum, Sirmio, insularumque ocelle, quascumque in liquentibus stagnis marique vasto fert uterque Neptunus, quam te libenter quamque laetus inviso, vix mi ipse credens Thuniam atque Bithunos liquisse campos et videre te in tuto. o quid solutis est beatius curis, cum mens onus reponit, ac peregrino labore fessi venimus larem ad nostrum, desideratoque acquiescimus lecto? hoc est quod unum est pro laboribus tantis. salve, o venusta Sirmio, atque ero gaude gaudente, vosque, o Lydiae lacus undae, ridete quidquid est domi cachinnorum.';
    if verLessThan('matlab','8.1') ==1
    warning('This function uses routine strjoin and works just with Matlab >=2013a')
    else
    Newstr=wraptextFS(str,'comment',true,'startcolumn',10,'width',40)
    end

  • Add an indentation for first line.
  • str='Paene insularum, Sirmio, insularumque ocelle, quascumque in liquentibus stagnis marique vasto fert uterque Neptunus, quam te libenter quamque laetus inviso, vix mi ipse credens Thuniam atque Bithunos liquisse campos et videre te in tuto. o quid solutis est beatius curis, cum mens onus reponit, ac peregrino labore fessi venimus larem ad nostrum, desideratoque acquiescimus lecto? hoc est quod unum est pro laboribus tantis. salve, o venusta Sirmio, atque ero gaude gaudente, vosque, o Lydiae lacus undae, ridete quidquid est domi cachinnorum.';
    if verLessThan('matlab','8.1') ==1
    warning('This function uses routine strjoin and works just with Matlab >=2013a')
    else
    Newstr=wraptextFS(str,'comment',true,'startcolumn',10,'width',40,'firstline',true)
    end

  • Use the width of command window.
  • str='Paene insularum, Sirmio, insularumque ocelle, quascumque in liquentibus stagnis marique vasto fert uterque Neptunus, quam te libenter quamque laetus inviso, vix mi ipse credens Thuniam atque Bithunos liquisse campos et videre te in tuto. o quid solutis est beatius curis, cum mens onus reponit, ac peregrino labore fessi venimus larem ad nostrum, desideratoque acquiescimus lecto? hoc est quod unum est pro laboribus tantis. salve, o venusta Sirmio, atque ero gaude gaudente, vosque, o Lydiae lacus undae, ridete quidquid est domi cachinnorum.';
    if verLessThan('matlab','8.1') ==1
    warning('This function uses routine strjoin and works just with Matlab >=2013a')
    else
    startcolumn=10;
    cms = get(0,'CommandWindowSize');
    width = cms(1)-10;
    Newstr=wraptextFS(str,'comment',false,'startcolumn',startcolumn,'width',width)
    end

  • Example of input option comment supplied as structure.
  • Symbol '$$$' is included at the beginning of each row in column 5.

    % The width of the text is 60 and starts in column 12.
    comment=struct;
    comment.commentsign='$$$';
    comment.startcolumn=5;
    startcolumn=12;
    width=60;
    str='Paene insularum, Sirmio, insularumque ocelle, quascumque in liquentibus stagnis marique vasto fert uterque Neptunus, quam te libenter quamque laetus inviso, vix mi ipse credens Thuniam atque Bithunos liquisse campos et videre te in tuto. o quid solutis est beatius curis, cum mens onus reponit, ac peregrino labore fessi venimus larem ad nostrum, desideratoque acquiescimus lecto? hoc est quod unum est pro laboribus tantis. salve, o venusta Sirmio, atque ero gaude gaudente, vosque, o Lydiae lacus undae, ridete quidquid est domi cachinnorum.';
    if verLessThan('matlab','8.1') ==1
    warning('This function uses routine strjoin and works just with Matlab >=2013a')
    else
    Newstr=wraptextFS(str,'comment',comment,'startcolumn',startcolumn,'width',width);
    end

  • Example of use of option code.
  • str='Paene insularum, Sirmio, insularumque ocelle, quascumque in liquentibus stagnis marique vasto fert uterque Neptunus, quam te libenter quamque laetus inviso, vix mi ipse credens Thuniam atque Bithunos liquisse campos et videre te in tuto. o quid solutis est beatius curis, cum mens onus reponit, ac peregrino labore fessi venimus larem ad nostrum, desideratoque acquiescimus lecto? hoc est quod unum est pro laboribus tantis. salve, o venusta Sirmio, atque ero gaude gaudente, vosque, o Lydiae lacus undae, ridete quidquid est domi cachinnorum.';
    if verLessThan('matlab','8.1') ==1
    warning('This function uses routine strjoin and works just with Matlab >=2013a')
    else
    out=xmlreadFS('tclust');
    ii=2;
    startcolumnEx=5;
    endcolumn=60;
    Ex=out.Ex;
    comment=struct;
    comment.commentsign='%';
    comment.startcolumn=startcolumnEx;
    endcolumnEx=60;
    i=2; jj=3;
    Exi=strtrim(Ex{i,jj});
    Exi{3,1}='%                        This is an example with extra spaces on the left which are trimmed';
    Exi{10,1}='for i=1:10';
    Exi{11,1}='    disp(i)';  
    % In this case the extra space on the left is wanted and it is not deleted
    Exi{12,1}='end';
    Eximod=Exi;
    for ii=1:size(Exi,1)
    % We must check whether it is comment or not
    % If it is a comment if the first character is symbol %
    Exii=Exi{ii,1};
    if ~isempty(Exii)
    if strcmp(Exii(1),'%')
    % In this case strtrim is invoked inside wraptextFS (code is
    % false)
    descriFormatted=wraptextFS( Exii(2:end),'startcolumn',startcolumnEx,'endcolumn',endcolumn,'firstline',false,'comment',comment,'code',false);
    else
    descriFormatted=wraptextFS( Exii,'startcolumn',startcolumnEx,'endcolumn',endcolumnEx,'firstline',false,'comment',false,'code',true);
    end
    end
    Eximod{ii,1}=descriFormatted;
    end
    % Before formatting
    disp(Exi)
    % After formatting
    disp([Eximod{:}])
    end

    Input Arguments

    expand all

    str — Input text. Character vector.

    String which has to be analysed and formatted.

    Data Types: single| double

    Name-Value Pair Arguments

    Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

    Example: 'startcolumn',10 , 'endcolumn',50 , 'width',50 , 'firstline',true , 'comment',true , 'code',false

    startcolumn —Left margin of the text.scalar (non negative integer).

    This option controls the left margin of the text. The default value of startcolumn is 1.

    Example: 'startcolumn',10

    Data Types: double

    endcolumn —Right margin of the text.scalar (non negative integer).

    This option controls the right margin of the text.

    The default value of endcolumn is 75.

    Example: 'endcolumn',50

    Data Types: double

    width —width of the text.scalar (non negative integer).

    This option controls the width of the text.

    The default value of width is 65.

    Remark: it is necessary just to give two values among, width, startcolumn and endcolumn because the third is automatically determined

    Example: 'width',50

    Data Types: double

    firstline —indentation for first line.boolean.

    If firstline is true then the first line starts in column 3 and not in column startcolumn, while the text in all the other columns starts as specified by option startcolumn.

    The default value of firstline is false

    Example: 'firstline',true

    Data Types: Boolean

    comment —specify whether text is a Matlab comment.boolean | structure.

    If comment is true then the first character in each row will be the percentage sign (comment symbol in Matlab). The default value of comment is false. If comment is a structure it is possible to personalize the symbol to put in from of each row, and the left margin of the comment symbol. More precisely, if comment is a structure it may contain the following fields:

    Value Description
    commentsign

    character(s) to be put at the beginning of each row. String which identifies comment sign.

    startcolumn

    starting column to include commentsign.

    Example: 'comment',true

    Data Types: Boolean

    code —specify whether text is a Matlab code (with comments).boolean.

    If code than the extra space on the left is not trimmed. The default value of code is false. Option code must be set to true when we have to translate to .m file code which contains wanted indentation.

    Example: 'code',false

    Data Types: Boolean

    Output Arguments

    expand all

    strFormatted —Output text. Character

    Formatted string.

    Text starts in column specified by option startcolumn, the first line may have an indentation, and length of the text in each row cannot exceed the prespecified width.

    References

    Acknowledgements

    This file had been inspired by function wraptext written by Chad A. Greene of the University of Texas https://www.mathworks.com/matlabcentral/fileexchange/53176-wraptext

    This page has been automatically generated by our routine publishFS