A format specifier cannot contain only literal text. Include a format specifier that contains a '%' character

Post Reply
matlab1
Site Admin
Posts: 4797
Joined: Thu Dec 15, 2016 9:30 am
Contact:

A format specifier cannot contain only literal text. Include a format specifier that contains a '%' character

Post by matlab1 »

A format specifier cannot contain only literal text. Include a format specifier that contains a '%' character
matlab1
Site Admin
Posts: 4797
Joined: Thu Dec 15, 2016 9:30 am
Contact:

Re: A format specifier cannot contain only literal text. Include a format specifier that contains a '%' character

Post by matlab1 »

Solution:


Formatting Text
To convert data to text and control its format, you can use formatting operators with common conversion functions, such as num2str and sprintf. These operators control notation, alignment, significant digits, and so on. They are similar to those used by the printf function in the C programming language. Typical uses for formatted text include text for display and output files.

For example, %f converts floating-point values to text using fixed-point notation. Adjust the format by adding information to the operator, such as %.2f to represent two digits after the decimal mark, or %12f to represent 12 characters in the output, padding with spaces as needed.

Code: Select all

A = pi*ones(1,3);
txt = sprintf('%f | %.2f | %12f', A)

You can combine operators with ordinary text and special characters in a format specifier. For instance, \n inserts a newline character.

Code: Select all

txt = sprintf('Displaying pi: \n %f \n %.2f \n %12f', A)

For more information,
https://www.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests