Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
143 views

User Controlled Input and Output MATLAB

This document discusses user input and output in MATLAB. It provides examples of taking input from the command line, loading data files, and reading Excel spreadsheets. It also demonstrates using the fprintf function to format output with more control than the basic disp command. Specifically, it shows how to print output with fixed point, exponential, or decimal notation and customize the precision of values printed. Finally, it creates a table converting between yards and inches to illustrate formatting output into a neat table.

Uploaded by

emongoloide
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views

User Controlled Input and Output MATLAB

This document discusses user input and output in MATLAB. It provides examples of taking input from the command line, loading data files, and reading Excel spreadsheets. It also demonstrates using the fprintf function to format output with more control than the basic disp command. Specifically, it shows how to print output with fixed point, exponential, or decimal notation and customize the precision of values printed. Finally, it creates a table converting between yards and inches to illustrate formatting output into a neat table.

Uploaded by

emongoloide
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UsercontrolledinputandOutput

Ref.Moorech.7
Review:
Wehaveusedtheseinputoptions:
Directinputatcommandline
Readinputusingtheinputcommand
Loadeddatafilesusingtheloadcommand
LoadedEXCELspreadsheetsdirectlyintoMATLAB

Recapusingtheinputcommand
e.g.thisMATLABinput
>>z=input('Whatisthenameofthisclassinquotes')
Whenpromptedtype:ME350
MATLABwillechothefollowing
z=
ME350
Wesawexamplesforinputtingscalarvalues
Nowsupposewewanttogetpromptedtoentervaluesinsquarebrackets
correspondingtoasimpleandfamiliar3by3matrix:
>>A=('inputthevaluesofamatrixinsquarebrackets')
A=
inputthevaluesofamatrixinsquarebrackets
1|P a g e

>>A=[1,2,3;4,5,6;7,8,9]
A=
123
456
789

Formattedoutputfprintffunction
Thisallowsustodisplayoutputinformationwithbettercontrolthanthebasic
dispcommand.Specificemphasiswillbebasedonthefieldformatandthe
precisionlevelwewouldbeinterestedinreporting
Commonlyformatfieldformatsinclude:
%f

fixedpointnotation

%e exponentialnotation
%d decimalnotationfordisplayingintegers
%g

Thiseffectivelybecomestheshorterofeitherthe%forthe%eformat

Seetable7.1,p.249fordetails

ThedefaultformatinMATLABistheshort%gformatwhichcorrespondstofive
significantdigits.
>>formatshort
>>x=pi
x=
3.1416

2|P a g e

>>formatlong
>>pi
ans=
3.141592653589793

Considerthefollowingcodesnippet
>>cows=6;
>>fprintf ('There are %d cows in the pasture', cows)

Willresultinthefollowingbeingechoedinthecommandwindow
There are 6 cows in the pasture

Payattentiontotheroleoftheparentheses,thesinglequotes,andthecoma
Changethesnippetto:
cows = 6;
fprintf ('There are %f cows in the pasture', cows)

Willresultinthefollowingbeingechoedinthecommandwindow
There are 6.000000 cows in the pasture

Nowaddmorefunctionalitytotheoutputcontrol,specifically,introducea
specialformatcommand\nwhichintroducesalinefeedbetweenoutputs:

3|P a g e

>>cows = 6;
>>fprintf ('There are %f cows in the pasture \n', cows)
>>pigs = 12;
>>fprintf ('There are %f pigs in the pasture \n', pigs)

Willresultinthefollowingbeingechoedinthecommandwindow

There are 6.000000 cows in the pasture


There are 12.000000 pigs in the pasture

SeesummaryofotherspecialformatcommandsinTable7.2,p.250

Nexttweaktheprecisionofinfotobeprintedandwatchthebuiltindata
integritysafety.Reviewthissnippetofcode:

cows = 6;
fprintf ('There are %4.4f cows in the pasture \n',
cows)
pigs = 12;
fprintf ('There are %4.2f pigs in the pasture \n',
pigs)

Theoutputis:
There are 6.0000 cows in the pasture
There are 12.00 pigs in the pasture

4|P a g e

Observations:

Inthecowsformat,wereservedatotalfourfields,allfourreservedforinfo
afterthedecimalpoint.
Inthepigsformatwereservedatotalof4fieldsandtwofieldsreservedfor
infoafterthedecimalpoint
MATLABhasbuiltinsafetymeasurestotrytopreservetheintegrityofdatain
theeventthatariskyfprintfformatthatcouldlosedataintegrityisinvokedin
thecode.

5|P a g e

Createaconversiontableofyardstoinches

yards = 0.1:0.1:1.0;
inches =yards.*36;
fprintf ('%4.2f \n', yards)
fprintf ('%5.2f \n', inches)

Theoutcomeshouldbealongcolumnsvectorwith10valuesofyardsand10
valuesofinchesappendedendtoend.
Amoreconvenientandnowfamiliartoolistocreateatableofdatafirstthen
useonefrpintfformatcommandtoprintthetable.

yards = 0.1:0.1:1.0;
inches =yards.*36;
%fprintf ('%4.2f \n', yards)
%fprintf ('%5.2f \n', inches)
%create a table
table=[yards;inches];
%Combine previous fprintf commands into one, to print
%the table
disp('inches yards')
fprintf ('%4.2f %5.2f \n', table)
Here is the outcome
yards inches
3.60 0.10
7.20 0.20
10.80 0.30
14.40 0.40
18.00 0.50
21.60 0.60
25.20 0.70
28.80 0.80
32.40 0.90
36.00 1.00

FurtherpracticalusesandillustrationswillbepartofAssignment4
6|P a g e

You might also like