
Notice that in the fprintf solution, I insert a newline so that the command prompt > goes underneath your text when displayed. The sprintf function is similar to the fprintf, but instead of writing text to the command window or a le Example: Simple string message > n 3 y sin(npi/2) > s sprintf(’The sine of d pi/2 is -6.4f’,n,y) s The sine of 3 pi/2 is -1.0000 sprintf is useful for creating le names that contain.

OR function = varInfo (numVec)įprintf('This variable is of class %s and has a value of ''%d''.\n', cLass, numVec) ĭesInput = sprintf('This variable is of class %s and has a value of ''%s''.', cLass, var1) įprintf('This variable is of class %s and has a value of ''%s''.\n', cLass, var1) Therefore, do either this: function = varInfo (numVec)ĭesInput = sprintf('This variable is of class %s and has a value of ''%d''.', cLass, numVec) The format string specifies notation, alignment, significant digits, field width, and other aspects of output format. str 'Hello World' sprintf('s',str) Output: ans 'Hello World' We can format the variable and then display it using the sprintf() function just like the fprintf() in the C language. sprintf is the same as fprintf except that it returns the data in a MATLAB string variable rather than writing it to a file. For example, let’s display a variable containing a string in MATLAB. sprintf() sprintf means print formatted text to string. As such, either remove the num2str call, or use %s as the modifier when displaying your number. You can use the sprintf() function to display a string in MATLAB.


If you want to display it, either use fprintf, or use disp(desInput) on your created string when using sprintf.Īlso, you want to display the value numVec, but you are converting it to a string before display. The C standard and POSIX specify that the behavior of sprintf and its variants is undefined when an argument overlaps with the destination buffer. Sprintf generates a string of a desired formatting and places it into a variable.
