intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Lecture 4: Input and output

Chia sẻ: BA AB | Ngày: | Loại File: PPT | Số trang:70

61
lượt xem
0
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

To print a variable in your display on the command window, its place must be indicted by % in the format-string followed by the format of presentation (d, f, e, g). %d: integer notation %f: fixed point (decimal) notation %e: exponential notation %g: whichever is shorter, %f or %e

Chủ đề:
Lưu

Nội dung Text: Lecture 4: Input and output

  1. Lecture 4 Input and Output  Input   input fprintf 1 © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
  2. Input  Input  Some options: – Assign values to variables in a command line Assign using the equal ( = ) sign. – Use the input function for interactive Use assignment of values to some of the variables within your computer program. within 2
  3. Input function Input  General format: variable = input('Please enter a value: ')  The display in the Command window: Please enter a value:  Type a value after the displayed prompt; Type the variable now has been assigned the value that was entered. value 3
  4. Hands on Area of a circle:  – Type this into an m-file: radius = input('Please enter the radius: ') radius input('Please area = pi*radius^2 – Execute and see this display in Command Window: Please enter the radius: 5.2 radius = 5.2000 5.2000 area = 84.9487 84.9487 4
  5. Hands on Free falling object:  Type this into an m-file:  height_0=input('Please enter the initial height (in meters): '); height_0= time=input('Please enter the time the object was in the air (in seconds): '); velocity_0=input('Please enter the initial velocity (in meters): '); a=9.81; height_f=height_0+velocity_0*time+(1/2)*a*time^2 Execute: See this display in Command Window:  Please enter the initial height (in meters): 25 Please enter the time the object was in the air (in seconds): 3 Please enter the initial velocity (in meters): 5 height_f = 84.1450 84.1450 5
  6. Input strings of text Input  General format:  string = input('Please enter your name: ', 's') string  The display in the Command window: Please enter your name:  This is used when the input is a string  (e.g., names, months, etc.). The variable,  in this case, is a  ‘char’ class (or type). 6
  7. Output options  The display variables: – You can display the value of a variable in You different ways. different  Typing “variable =” in the command window x=500 x= 500 500  Using the fprintf command: – Type into an m-file: x=500; fprintf('The value of x is %f.\n',x) – Executing it displays in the Command Window: The value of x is 500. 7
  8. fprintf() fprintf()  The fprintf() command is one way to The fprintf() display the value of a variable with a label. display  General format:  MATLAB code: MATLAB fprintf('format-string', variable) fprintf('format-string', Note: You can also use the disp() function to display Note: results. See the help on disp() and the examples in the text. the 8
  9. Placeholders in fprintf() fprintf()  To print a variable in your display on the To command window, its place must be indicted by % in the format-string followed by the format of presentation (d, f, e, g). by  %d: integer notation integer  %f: fixed point (decimal) notation fixed  %e: exponential notation exponential  %g: whichever is shorter, %f or %e 9
  10. Hands on Type into an m-file:  smiles=7 smiles=7 fprintf('Sarah smiles %d times a day.',smiles) smiles day.',smiles Executing it displays in the command window:  smiles = 7 Sarah smiles 7 times a day. 10
  11. Hands on Type into an m-file:  month=input('Please enter your month of birth (i.e. May): ', 's'); month= day=input('Please enter your number day of birth: '); fprintf('Your birthday is %s %d!!',month,day) Executing it displays in Command Window:  Please enter your number month of birth (i.e. May): August Please enter your number day of birth: 11 Your birthday is August 11!! 11
  12. Format  When fprintf is used consecutively, When fprintf MATLAB prints the results on the same line in the command window. smiles=7; smiles=7; fprintf('Sarah smiles %d times a day.',smiles) smiles day.',smiles – After the second execution of this command: Sarah smiles 7 times a day.Sarah smiles 7 times a day.>> 12
  13. The \n command The \n  This command is a linefeed. It commands  MATLAB to start on a new line so that  sequential outputs are on separate lines.  – Example waffles=4; waffles=4; fprintf('Erin ate %d waffles today.\n',waffles) Erin ate 4 waffles today. >> – Be sure to use \n, not /n. 13
  14. Width & precision fields Width  The width field specifies the minimum The width number of characters to be printed. number  The precision field specifies the number of The precision those characters that will show up after the decimal point. decimal 14
  15. Hands on %8.2f specifies that there can be no less than 8  characters in the displayed output, and that two of them are to follow the decimal point. weight= 57638.75; weight= fprintf('The weight is %8.2f pounds \n', weight) weight fprintf(‘The weight is %50.2f pounds \n’, weight) weight The weight is 57638.75 pounds The weight is 57638.75 pounds – Notice the blank spaces in the second output. – Use %% to have a % sign show up in output. Use %% 15
  16. Summary I/O Summary I/O input  – variable = input('Please enter a value: ') – string = input('Please enter a value: ', 's') fprintf  – fprintf('format­string', variable) %f = fixed point (decimal) notation – %e = exponential notation – %g = whichever is shorter, %f or %e – 16
  17. Lecture 5 MATLAB functions Basics of Built-in Functions, Basics Help Feature, Help Elementary Functions (e.g., Polynomials, Elementary Trigonometric Functions), Data Analysis, Random Numbers, Complex Numbers Complex 17 © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
  18. What is a built-in function? What A computational expression that uses one or computational  more input values to produce an output value. more MATLAB Functions have 3 components:  input, output, and name output name b = tan(x) tan( x is the input, b is the output, and tan is the input output and tan name of a built-in function; iin this case, it is the n name tangent function with argument in radians. tangent 18
  19. MATLAB functions Functions take the form:  variable = function(number or variable) variable MATLAB has many functions stored in its file MATLAB  system. system. To use one of the built-in functions, you need to To  know its name and what the input values are. know For example, the square root function: sqrt(). For square  Find the square root of 9 using MATLAB  a = sqrt(9) sqrt(9) 19
  20. HELP feature HELP  You may not always recall the name and You syntax of a function you want to use since MATLAB has so many built-in functions. MATLAB  MATLAB has an indexed library of its built- iin functions that you can access through n the help feature. help  If you type help in the command window If help MATLAB provides a list of help topics. MATLAB 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD


ERROR:connection to 10.20.1.100:9315 failed (errno=111, msg=Connection refused)
ERROR:connection to 10.20.1.100:9315 failed (errno=111, msg=Connection refused)

 

Đồng bộ tài khoản
2=>2