![Page 1: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/1.jpg)
General Computer General Computer Science Science
for Engineersfor EngineersCISC 106CISC 106
Lecture 07Lecture 07
Dr. John CavazosComputer and Information Sciences
2/25/2009
![Page 2: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/2.jpg)
Lecture OverviewLecture OverviewMore Emacs
Reading in and editing filesPlotting
Simple PlotsMatlab talks back to you
Using fprintf to display output.
![Page 3: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/3.jpg)
EmacsEmacsOpening a new file
emacs newfile.cppCreates a blank file named
newfile.cpp for editingIf new file already exists
Emacs newfile.cpp opens file for editing
Save edited file by using GUI or by command Ctrl x followed by Ctrl w.
![Page 4: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/4.jpg)
EmacsEmacsLet’s create a file with two arrays.
![Page 5: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/5.jpg)
PlotPlotUsing the plot command
plot(<array 1>,<array 2>)
where array1 is the x-axis and array2 is the y-axis
NOTE: array1 and array2 must be equal in terms of size of dimensions!
![Page 6: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/6.jpg)
PlotPlotFor example:
x=[1 2 3 4 5];y=[10 20 30 40 50];
plot(x,y)
![Page 7: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/7.jpg)
PlotPlotOther useful command with plotxlabel(‘<string>’) – sets the label for
the x-axisylabel(‘<string>’) – sets the label for
the y-axisgrid on – creates a grid title(‘<string>’) – sets title of the plot
![Page 8: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/8.jpg)
Matlab OutputUse the fprintf command
◦ print is intuitive, but fprintf?◦ fprintf – formatted print to a file
What do files have to do with output to the screen?
![Page 9: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/9.jpg)
Matlab OutputLets look at the syntax
count = fprintf(fid, format, A, ...)◦ count – the number of bytes written◦ fid – refers to opened file◦ format – is a format string in between single quotes
If fid is set to one or omitted the output goes to the screen.
![Page 10: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/10.jpg)
Matlab OutputExample:
◦ “There are 5 widgets in inventory”◦ fprintf(‘There are %d widgets in inventory’,
widgets);%d acts as a place holder for widget
variable at end of command and is known as a conversion character.
Conversion characters specify the notation of the output.
Note the variable widgets as the second argument.
![Page 11: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/11.jpg)
Matlab OutputUse the fprintf command
◦ print is intuitive, but fprintf?◦ fprintf – formatted print to a file
What do files have to do with output to the screen?
Lets look at the syntaxcount = fprintf(fid, format, A, ...)◦ count – the number of bytes written◦ fid – refers to opened file◦ format – is a format string
If fid is set to one or omitted the output goes to the screen.
![Page 12: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/12.jpg)
Matlab OutputExample
◦ “On average we sell 2.56 widgets every 2 weeks”
◦ fprintf(‘On average we sell %f widgets every %d weeks’, widgets_ave, num_weeks);
The conversion characters matter◦ %f for fixed point notation 5.6332◦ %c for a single character ‘s’◦ %d/%i for signed integers -2354◦ %s for a string of character “CISC106”◦ Even more conversion characters online in
Matlab reference.
![Page 13: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/13.jpg)
Matlab OutputExample
◦ “Line one Line two”
fprintf(“Line one \nLine two”); Is the same asfprintf(“Line one”);fprintf(“\nLine Two”);
Escape Characters◦ \n – new line \t – Horizontal tab◦ \b – backspace \\ - Backslash
![Page 14: General Computer Science for Engineers CISC 106 Lecture 07](https://reader035.vdocuments.us/reader035/viewer/2022070402/56813948550346895da0e553/html5/thumbnails/14.jpg)
Matlab OutputWhen MATLAB gives you ugly
numbers.....◦ Take pi for example
3.14159265358979323846Print pi to 4 decimal places
◦ fprintf(‘Pi to 4 decimal places %.4f’, pi);◦ 3.1415
Print pi to 7 significant digits◦ fprintf(‘Pi to AT LEAST 7 significant digits
%7f’, pi);◦ Significant digits - digits before the decimal
point