Lecture 21
Winter Quarter
Lect 21
P. 1
Less Than Less Than or Equal Greater Than Greater Than or Equal Equal To Not Equal To
Winter Quarter
Lect 21
P. 2
Winter Quarter
Lect 21
P. 3
Winter Quarter
A while loop in MATLAB while expression while x <= 10 % execute these commands end
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 21 P. 5
The . operator If you want to do element by element operation on a vector or matrix. Consider A = [2, 4, 5, 8] B = [1, 2, 2, 4] Then C = A./B is [2,2,2.5,2] And D = 1./A is [.5,.25,.2,.125]
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 21 P. 10
ASCII files that cannot be input with the load function can be opened and input with MATLAB functions that are similar to C language functions we have been using. The MATLAB functions include fopen, fgets, fscanf, and sscanf.
Winter Quarter
Lect 21
P. 11
infile_id = fopen('filename','r');
outfile_id = fopen('filename','w');
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 21 P. 12
The syntax to input a line and then extract and discard three columns from the string and save the fourth column of floating point data in an element in the array myarray would be:
line = fgets(infile_id); myarray(k)=sscanf(line,'%*s%*s%*s%f');
Winter Quarter The Ohio State University Gateway Engineering Education Coalition Lect 21 P. 14
myarray( i ) = 0.0;
end
Lect 21 P. 15
Lines of other data can be output by inserting the appropriate format string and variable names. For example: fprintf(outfile_id,'%f%f\n',a,b);
Winter Quarter
Lect 21
P. 16
fprintf('%f %f\n', a , b)
Winter Quarter
Lect 21
P. 17