TAILIEUCHUNG - Lecture 4: Input and output

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 | Lecture 4 Input and Output input fprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier. Input Some options: Assign values to variables in a command line using the equal ( = ) sign. Use the input function for interactive assignment of values to some of the variables within your computer program. Input function General format: The display in the Command window: Type a value after the displayed prompt; the variable now has been assigned the value that was entered. variable = input('Please enter a value: ') Please enter a value: Area of a circle: Type this into an m-file: Execute and see this display in Command Window: Hands on Please enter the radius: radius = area = Hands on Free falling object: Type this into an m-file: 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 = Input strings of text General format: The display in the Command window: This is used when the input is a string (., names, months, etc.). The variable, in this case, is a ‘char’ class (or type). string = input('Please enter your name: ', 's') Please enter your name: The display variables: You can display the value of a variable in different ways. Typing “variable =” in the command window Using the fprintf command: Type into an m-file: Executing it displays in the Command Window: Output options x = 500 The value of x is 500. fprintf() The fprintf() command is one way to display the value of a variable with a label. General format: MATLAB code: fprintf('format-string', variable) Note: You can also use the disp() function to display results. See the help on disp() and the examples in the text. Placeholders in fprintf() 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 .

TỪ KHÓA LIÊN QUAN
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.