MATLAB Input and Output & Decision Statement PDF

Summary

This document provides examples of MATLAB programming, focusing on input and output methods and decision-making statements. It covers topics such as MATLAB scripts, functions, and interactive commands. The document likely serves as a teaching aid or reference material for students or professionals.

Full Transcript

Modeling and Simulation Modeling and Simulation Construct MATLAB scripts. Create user-defined functions that returns a single value. Identify and use MATLAB basic input and output commands. Construct MATLAB scripts with selections and looping. SUBTOPIC 1 Input and Output Too...

Modeling and Simulation Modeling and Simulation Construct MATLAB scripts. Create user-defined functions that returns a single value. Identify and use MATLAB basic input and output commands. Construct MATLAB scripts with selections and looping. SUBTOPIC 1 Input and Output Toolstrip and Editor * Scripts are stored as.m The M Files MATLAB allows writing two kinds of program files: Scripts − script files are program files with.m extension. In these files, you write series of commands, which you want to execute together. Scripts do not accept inputs and do not return any outputs. They operate on data in the workspace. Functions −functions files are also program files with.m extension. Functions can accept inputs and return outputs. Internal variables are local to the function. Example of Scripts Examples of script s1 = ["Red" "Blue" "Green"] a = 1:2; s2 = ["Truck" "Sky" "Tree"] b = (1:3)'; s = s1 + s2 a+b A = [1 0; 2 4]; B = [5 9; 2 1]; C=A+B Examples of Using Input Commands (script) % entering an integer Output : input("Enter an integer : ") Enter an integer : 10 ans = 10 % entering a string input("Enter a string : ", "s") Enter a string : FEU_Tech ans = FEU_Tech x = input("Enter a number") y = input("Enter another number") z1 = x + y z2 = x - y z3 = x * y z4 = (x + y)/2 Functions −functions files are also program files with.m extension. Functions can accept inputs and return outputs. Internal variables are local to the function. x = input("Enter a number ") y = input("Enter another number ") z1 = x + y z2 = x - y z3 = x * y z4 = (x + y)/2 fprintf('\n Sum is. %d ',z1); fprintf('\n Difference is. %d',z2); fprintf('\n Product is. %d',z3); fprintf('\n Average is. %d',z4); disp(X) displays the value of variable X without printing the variable name. Another way to display a variable is to type its name, which displays a leading “X =” before the value. X = 'MathWorks disp(A) Web Site'; disp(X) S = 'Hello World.'; disp(S) fprintf to directly display the text without creating a variable. However, to terminate the display properly, you must end the text with the newline (\n) metacharacter. disp('Corn Oats Hay') name = 'Alice'; X = rand(5,3); age = 12; disp(X) fprintf('%s will be %d this year.\n',name,age); Input and Output Commands Input and Output Commands The fscanf and fprintf commands behave like C scanf and printf functions. They support the following format codes. Examples of Using Output Commands Interactively With formatting: Printing Vectors and Matrices Interactively Examples: Selection and Looping The IF Statement Example : Determine if the data is negative or Determine if the data is positive negative or positive Odd or even number.x = input("Enter a number "); if x < 0 x = input("Enter a number "); fprintf('Negative'); if x < 0 else fprintf('Negative'); fprintf('Positive'); else end fprintf('Positive'); end y = x % 2; if y == 0 fprintf('Even number'); else fprintf('Even number'); end Age problem age = input("Enter a age "); if age > 0 && age 13 && age 20 && age

Use Quizgecko on...
Browser
Browser