Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

MATLAB Output Functions

The output to the screen using ‘displaymatlab function.

            To display the result with new line and more than 1 argument or variable, I used the cell format to display the variables.
In the first example, I used cell format to display the variables.  The special character ‘,’ is used as a concatenation operator and the ‘;’ is used as a new line character.

%Simple Example to demonstrate display function
length=10;
breadth=30.4;
shape=' rectangle';

area=length*breadth;

result={'Length =',length; 'Breadth =',breadth;'Shape:',shape; 'Area of the rectangle =',area};
display(result);



Here I converted the single line into a string and then used ‘;’ for a new line character.

%Lets make use strings here

result={strcat('length of the',shape,'= ',num2str(length)); strcat('Breadth of the',shape,'=',num2str(breadth)); strcat('Area of the',shape,'=',num2str(area))};
display(result);






%sprintf

result=sprintf('Length of the %s = %.2f \nBreadth of the %s = %.2f \nArea of the %s = %.2f',shape,length,shape,breadth,shape,area);
display(result);




Sprintf is used to format the text. It is similar to the ‘C’ function ‘printf’. For strings, ‘%s’ is used. For decimals,’%d’ is used.
And for new line same as in ‘C’ we use ‘\n’ here.

like button Like "IMAGE PROCESSING" page

0 comments:

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com