Monday, October 29, 2012

fprintf and vector in MATLAB

Usually in MATLAB, when fprintf is called, the numerical input is a scalar rather than a vector. This MATLAB built-in function actually supports the display of a vector. The following is one of the examples given in the help,

B = [8.8 7.7; 8800 7700]  
fprintf(1, 'X is %6.2f meters or %8.3f mm\n', 9.9, 9900, B) 

These commands display
X is  9.90 meters or 9900.000 mm  
X is  8.80 meters or 8800.000 mm  
X is  7.70 meters or 7700.000 mm  

However, you may only want to print a vector. The following examples show how to use frpintf with a vector.
a =1:3;  
fprintf('%g', a)  
fprintf('%g|', a)  
fprintf('%2.2f,', a)  
The commands above give
Note that how the numbers are separated.

No comments:

Post a Comment