Download presentation
Presentation is loading. Please wait.
Published byBartholomew Long Modified over 6 years ago
1
Introduction to Programming for Mechanical Engineers (ME 319)
Lecture 4, Module 1
2
Quote of the day “Be the change you wish to see in the world” M.K.Gandhi
3
Things you should have known so far!
Create vectors and plot them together. Plot functions over an interval using the fplot command. Use plotting options such as specifiers. Assign different line and marker specifiers to different plots. Add text, legend, x-label, y-label, and title to a figure. Use other types of 2-D plots such as the Pie and the Polar coordinates.
4
Input to a script file >> Ch4_preparation_1 result = -4 -14
What if we need to change the elements in A or B? Is it convenient to open the file each time and change the values manually?
5
Input to a script file: input
>> Ch4_preparation_1 Enter the value of element A(1,1) 2 Enter the value of element A(1,2) -4 Enter the value of element A(2,1) -3 Enter the value of element A(2,2) 1 Enter the value of element B(1) 6 Enter the value of element B(2) 4 result = -14 So, it is more systematic way and more practical Use the syntax provided x = input(‘string’) Hit the enter key to assign the elements values to the variables. Use \n at the end of the string to take the cursor to the next line: x = input(‘string\n’)
6
Input to a script file: input
% in a script file A = [input(‘Element (1,1)’) input(‘Element (1,2)’) input(‘Element (2,1)’) input(‘Element (2,2)’)] Input data: In a script file. In the workspace. In a command through the workspace (using input).
7
Output commands: disp The disp command is used to display the elements of a variable without displaying the name of the variable. It displays the numerical value in the variable or a string of text. disp(name of variable) or disp(‘text as string’) Examples: >> a = 6; >> disp(a) 6 >> disp('ME319') ME319 After executing the script file, the following will be displayed in the workspace: The result of AxB is 17 39
8
Output commands: fprintf
Unlike disp, the output can be formatted using fprintf. Numerical values and text can be intermixed and displayed in the same line. Also, numbers can be formatted. >> fprintf ('UNLV, Mechanical Engineering, ME319') UNLV, Mechanical Engineering, ME319>> >> fprintf ('UNLV, Mechanical Engineering, \n ME319\n') UNLV, Mechanical Engineering, ME319 >> \n New line Continues the following string in a new line \t Horizontal tab Adds a horizontal tab \b Backspace Deletes the character before
9
Output commands: fprintf
To display a mix of text and numerical values, use the following syntax: fprintf (‘text as string %-5.2f additional text‘,variable_name) The initial text The % sign marks the spot where the variable value is inserted Name of variable to be displayed Formatting elements flag - (minus) Left justifies the number within text + (plus) Adds a plus sign in front of number 0 (zero) Adds zeros in the unused field spaces
10
Output commands: fprintf
To display a mix of text and numerical values, use the following syntax: fprintf (‘text as string %-8.3f additional text‘,variable_name) Name of variable to be displayed The initial text The % sign marks the spot where the number is inserted Formatting elements >> A = 2200/7; >> fprintf('the value is $\t %-8.3f \tdollrs\n',A) the value is $ dollrs >> fprintf('the value is $\t %08.2f \tdollrs\n',a) the value is $ dollrs >> Field and precision Field is the integer value (8) The number of used digits in the display Precision is the decimal value (3) (optional) The number of used digits to represent the decimal e Exponential notation lower case 3.14e+002 E Exponential notation upper case 3.14E+002 f Fixed point notation g The shorter of e or f notations G The shorter of E or f notations i Integer
11
Output commands: fprintf
fprintf (‘…text….%f….%f…%g… additional text‘,variable1, variable2, variable3,...) % Example (MATLAB script file): x = input('Enter the balance in dollars\n'); y = input('Enter the money deposited in dollars\n'); z = y - x; fprintf('If your balance is $ %5.2f \n and the money you deposited is $ %5.2f \n then the money you get back from cashier is $ %+5.2f\n',x,y,z) % WORKSPACE Enter the balance in dollars 2.41 Enter the money deposited in dollars 5 If your balance is $ 2.41 and the money you deposited is $ 5.00 then the money you get back from cashier is $ +2.59 >>
12
Output commands: fprintf
fprintf is vectorized, meaning that if the variable is a matrix or a vector, the command repeats itself till all the elements are displayed. >> people = 2:7; >> cost = linspace(10,6,6); >> cinema = [people;cost]; >> fprintf('If the number of people is\t %5.0f\t then the cost per ticket would be \t $%5.2f\t\n',cinema) If the number of people is then the cost per ticket would be $10.00 If the number of people is then the cost per ticket would be $ 9.20 If the number of people is then the cost per ticket would be $ 8.40 If the number of people is then the cost per ticket would be $ 7.60 If the number of people is then the cost per ticket would be $ 6.80 If the number of people is then the cost per ticket would be $ 6.00
13
Output commands: fprintf
Using fprintf to save output to a file Using fprintf to save output to a file: 1st step: open the file using the following syntax: fid = fopen('file_name_including_extension','permission'); 2nd step: use fprintf to write to the file using the following syntax: fprintf(fid, 'text %5.2f additional text',variable_name); 3rd step: close the file using the following syntax: fclose(fid); permission 'r' Opens a file for reading (default) 'w' Opens a file for writing. If the file exists, its content is deleted, if it doesn’t exist, it is created 'a' Same as 'w', except that if the file exists, the data is written at the end of the file 13
14
Output commands: fprintf
Using fprintf to save output to a file minimum_income = 20000:10000:70000; maximum_income = 29999:10000:79999; tax = linspace(5000,22000,6); Income = [minimum_income;maximum_income;tax]; fid1 = fopen('Income_to_tax.doc','w'); fprintf(fid1 ,'Income to tax table\n'); fprintf(fid1,'Minimum income per year Maximum income per year Tax\n\n'); fprintf(fid1,' $%8.2f \t $%8.2f $%8.2f\n',Income); fclose(fid1); Commonly used Extensions .doc Word document .txt Notepad document .xls Excel sheet .ppt Power point document 14
15
Output commands: sprintf
sprintf - Write formatted data to an output string Syntax: sprintf(‘ ’,’format_string’, value1,value2, ..., valueN) sprintf('The price of %s on %d/%d/%d was $%.2f.', ... 'bread', 7, 1, 2006, 2.49) ans = The price of bread on 7/1/2006 was $2.49.
16
Thank you for the attention
Any Questions and Suggestions please…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.