SoftwareGuide_MATLAB
SoftwareGuide_MATLAB
Input Statements:
Output Statements:
% Decimal Numbers
x = 75.176;
fprintf('The variable x = %0.2f \n',x);
% %0.2f means show two places behind decimal point
% Integers
y = 750;
fprintf('The value of y is: %i \n',y);
% Strings
course = 'Calculus II';
fprintf('The name of this course is: %s \n',course);
% Mixture
x = 75.176; y = 750; course = 'Calculus II';
fprintf('x = %0.1f, y = %i, course is %s \n',x,y,course');
Conditional Statements:
if Condition1
% MATLAB Commands to execute if Condition1 is TRUE
elseif Condition2
% MATLAB Commands to execute if Condition1 is FALSE
% and Condition2 is TRUE
elseif Condition3
% MATLAB Commands to execute if Condition1 is FALSE
% and Condition2 is FALSE
% and Condition3 is TRUE
else
% MATLAB Commands to execute if Conditions 1-3 are FALSE
end
For Loops:
Another Option
for k = 1:3:12
% MATLAB Commands
% Counter index, k, loops through the values 1, 4, 7, 10
end
While Loops:
while Condition
% MATLAB Commands will execute again and again as long as
% Condition is true
end
Arrays:
Plot:
x = -pi:pi/100:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,'-k','LineWidth',2)% plot x and y using line,
%color black (k) and
%thickness 2
Execute the Function at the Command Prompt or from another MATLAB program:
>> [Area,Perimeter]=Rectangle(3,4)
Area = 12
Perimeter = 14
Note: if you call the function from another function, both functions must be in the same folder.
Open a File:
fid = fopen(‘filename.txt’,PERMISSION)
PERMISSION:
'r' open file for reading
'w' open file for writing; discard existing contents
'w+' open or create file for reading and writing;
discard existing contents
Read from File:
A = fscanf(fid,formatSpec,sizeA)
Write to File:
fclose(fid)