Formatted output

advertisement
EGR 106 – Truss Design Project (cont.)





Truss design programs
Graphical interface tools in Matlab
Saving and loading data
Formatted output
Project Assignment – part #2
Examples of truss design programs
Johns Hopkins University:
http://www.jhu.edu/~virtlab/bridge/truss.htm
West Point Bridge Design Contest
http://bridgecontest.usma.edu/
Graphical Interface Tools


The function draw_truss will be provided to
allow for graphical input of joint coordinates
and member definitions
To use this function:
–
–
Download the file draw_truss.m from the course
web page and save it in your current directory
Include the following command in your code:
[joint_def,member_def]=draw_truss
Graphical Interface:
the function draw_truss
Pick member 10, joint 1 (right click to stop)
200
150
100
50
0
-50
-100
-150
-200
0
50
100
150
200
250
300
350
400
Importing/Exporting Data

Saving data:
–
–
–
save filename
save filename array1 array2
save filename –ascii

Reading in (loading) data:
–
–
–
load filename
load filename array1 array2
load filename –ascii
Importing/Exporting Data - example
Saving arrays to file:
Loading arrays from a file:
>> clear
>> a=[1 2 3; 4 5 6]
a =
1
2
3
4
5
6
>> b=[7 8 9]
b =
7
8
9
>> save arrays_a_and_b a b
>> clear
>> load arrays_a_and_b a b
>> a
a =
1
2
3
4
5
6
>> b
b =
7
8
9
Creates file arrays_a_and_b.mat
Formatted output

Formatted print command: fprintf(format,data)
–
–
–
–
Combines text and data
Allows complete control of the output
format is a string (in quotes) containing text and control
commands (using \ or %)
See text pages 87-94 for more details
Formatted output (cont.)
An example:
The data itself
\n = new line
%d = put data here
Formatted output (cont.)
Escape characters:
– New line ( \n ), backspace ( \b ), tab ( \t ), …
Formatted output (cont.)
Conversion codes for data:
–
Decimal ( %d ), character ( %c ), exponential notation ( %e ),
fixed precision ( %f ), …
–
Can add field width and precision information:
5 characters wide, 2
after the decimal point
Formatted output (cont.)
fprintf works element-by-element on array data:
\n added for
line return
Formatted output to a file
To write to a file:
fid = fopen( 'name', 'w' )
fprintf(fid,format,data)
…
fclose(fid)
–
–
w indicates
“write”
fid is a variable name to specify which file
name is the name of the file
Truss Project – Assignment #2*
Starting with your code from assignment #1:
–
–
–
–
–
–
–
Allow user to load results from previous design
Use function draw_truss to create new design
Develop code to allow user to define boundary conditions, forces
and member width and thickness
Compute total length of members, mass of each member and total
mass of truss
Allow user to save truss design
Have code create output file summarizing design parameters
Look for additional features to make your code more user friendly
* See handout for details
Download