CH 04
CH 04
2
4.1 THE MATLAB WORKSPACE AND THE WORKSPACE WINDOW
4
4.1 THE MATLAB WORKSPACE AND THE WORKSPACE WINDOW
5
4.1 THE MATLAB WORKSPACE AND THE WORKSPACE WINDOW
6
4.1 THE MATLAB WORKSPACE AND THE WORKSPACE WINDOW
7
4.1 THE MATLAB WORKSPACE AND THE WORKSPACE WINDOW
9
4.2 INPUT TO A SCRIPT FILE
10
4.2 INPUT TO A SCRIPT FILE
11
4.2 INPUT TO A SCRIPT FILE
13
4.2 INPUT TO A SCRIPT FILE
14
4.2 INPUT TO A SCRIPT FILE
15
4.2 INPUT TO A SCRIPT FILE
variable_name=input('prompt')
prompt is text that input command
displays in Command Window
• You must put text between single quotes
16
4.2 INPUT TO A SCRIPT FILE
variable_name=input('prompt')
When script executes input command
1. Displays prompt text in Command
Window
2. Puts cursor immediately to right of
prompt
3. User types value and presses ENTER
4. Script assigns user's value to variable
and displays value unless input
command had semicolon at end
17
4.2 INPUT TO A SCRIPT FILE
18
4.2 INPUT TO A SCRIPT FILE
20
4.2 INPUT TO A SCRIPT FILE
Joe
21
4.2 INPUT TO A SCRIPT FILE
Method 2
Pass 's' as second argument to input.
User should not enter quotes
variable_name=input('prompt', 's')
>> name=input('Your name: ', 's')
Your name: Joe User enters without quotes
name =
Joe
22
4.3 OUTPUT COMMANDS
23
4.3.1 The disp Command
24
4.3.1 The disp Command
25
4.3.2 The fprintf Command
fprintf
• Means file print formatted
formatted text is text that can be read by people
unformatted text looks random to people but
computers can read it
• Can write to screen or to a file
• Can mix numbers and text in output
• Have full control of output display
• Complicated to use
26
4.3.2 The fprintf Command
28
4.3.2 The fprintf Command
29
4.3.2 The fprintf Command
30
4.3.2 The fprintf Command
Format string
31
4.3.2 The fprintf Command
Format string
• May contain text and/or conversion
specifiers
• Must be enclosed in SINGLE quotes,
not double quotes, aka quotation
marks (" ")
32
4.3.2 The fprintf Command
Arguments
• Number of arguments and conversion
specifiers must be the same
• Leftmost conversion specifier formats
leftmost argument, 2nd to left specifier
formats 2nd to left argument, etc.
33
4.3.2 The fprintf Command
Conversion specifier
>> fprintf( 'Joe weighs %f kilos', n1 )
34
4.3.2 The fprintf Command
Conversion specifier
>> fprintf( 'Joe weighs %6.2f kilos', n1 )
>> e = exp( 1 );
>> fprintf( 'e is about %4.1f\n', e )
e is about 2.7
>> fprintf( 'e is about %10.8f\n', e )
e is about 2.71828183
>> fprintf( 'e is about %10.8e', e )
e is about 2.71828183e+000
>> fprintf( 'e is about %10.2e', e )
e is about 2.72e+000
>> fprintf( 'e is about %f\n', e )
e is about 2.718282
36
4.3.2 The fprintf Command
37
4.3.2 The fprintf Command
38
4.3.2 The fprintf Command
39
4.3.2 The fprintf Command
Example
>> weight = 178.3;
>> age = 17;
>> fprintf( ['Tim weighs %.1f lbs'...
' and is %d years old'], weight, age )
40
4.3.2 The fprintf Command
41
4.3.2 The fprintf Command
42
4.3.2 The fprintf Command
43
4.3.2 The fprintf Command
44
4.3.2 The fprintf Command
Step b:
Write to file with fprintf. Use it
exactly as before but insert fid
before the format string, i.e.,
fprintf(fid,'format
string',variables)
Step c:
When you're done writing to the file,
close it with the command
fclose(fid)
• Once you close it, you can't use that
fid anymore until you get a new
one by calling fopen
Make sure to close every file you
open. Too many open files
creates problems for MATLAB
46
4.3.2 The fprintf Command
Miscellaneous
• If the file name you give to fopen has no
path, MATLAB writes it to the current
directory, also called the working directory
• You can have multiple files open
simultaneously and use fprintf to write to
all of them just by passing it different fids
• You can read the files you make with
fprintf in any text editor, e.g., MATLAB's
Editor window or Notepad
47
4.4 THE SAVE AND LOAD COMMANDS
48
4.4 THE SAVE AND LOAD COMMANDS
49
4.4 THE SAVE AND LOAD COMMANDS
50
4.4.1 The save Command
51
4.4.1 The save Command
52
4.4.2 The load Command
55
4.5.1 Commands for Importing and Exporting Data
56
4.5.1 Commands for Importing and Exporting Data
xlswrite('filename',variable_name)
58