New user question on fprintf

1 view (last 30 days)
Hey, i'm a new user to Matlab and am working on some homework. I'm trying to figure out how to write a script that will prompt the user to input a character and then perform the following actions.
A. use fprintf to print the character without specifying a field width
B. use fprintf to print the character specifying a field width of 5
My code I've been working on is below, you can see my mistakes.
% Prompt user to enter character
disp ('Please enter a character...any character')
%STRvar = input('Please enter a character', 's')
%fprintf (' var1 is %3.1s/')
%character = input(' Enter a character: ', 's');
fprintf ('The character %d has no specified field width\n')
If anyone could help me it'd be appreciated, thanks!

Accepted Answer

Star Strider
Star Strider on 1 Feb 2015
See the documentation on fprintf (and optionally for Formatting Strings).
In this line:
fprintf ('The character %d has no specified field width\n')
you need to use the correct format descriptor to print a character (see the fprintf documentation for that), and you need to tell the fprintf function the character you want it to print. Other than that, you are where you want to be with it.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!