Octave Notes

Plotting with GNUPlot

Changing a plot title

To change the title of a plot use the following:

title('New Title');
replot();

Saving a plot to a file

To save a plot to a png file for printing or manipulating use the following (this code assumes your plotting has already been done):

gset terminal png
gset output "filename.png"
replot

To save the same plot in postscript use the following:

gset terminal postscript
gset output "filename.ps"
replot

File I/O

Saving an Array to a File

Assume you have an empty file, called data.txt, and a three dimensional array, A, of floating point numbers, use the following commands:

fid = fopen("data.txt","w"); % opens the file data.txt for writing, saves the file pointer as fid.
fprintf(fid,"%f %f %f\n",A); % prints the three elements in each row of array A into a line in fid.
fclose(fid);                 % flushes the buffer and closes the file.

Loading an Array from a File

To load the data in this file we saved in the last section to a vector array, B, use the following commands:

fid = fopen("data.txt","r"); % opens file data.txt for reading, saves the file pointer as fid
B = fscanf(fid,"%f");        % reads through the file, matching the pattern "%f", and stores the floats into a vector array called B
B = B';                      % transposes B, turning it from an n*1 array to a 1*n array (this step is optional)
fclose(fid);                 % closes the file

To load the data in this file we saved in the last section to a new 3 * 3 array, C, use the following commands:

fid = fopen("data.txt","r");        % opens file data.txt for reading, saves the file pointer as fid
C = fscanf(fid,"%f %f %f\n",[3 3]); % reads through the file, matching the pattern "%f %f %f\n", and stores the floats into a 3*3 array ([3 3]), called C
fclose(fid);                        % closes the file

To load an unspecified length three*n array, D, you would use the following:

fid = fopen("data.txt","r");          % opens file data.txt for reading, saves the file pointer as fid
D = fscanf(fid,"%f %f %f\n",[3 inf]); % reads through the file, matching the pattern "%f %f %f\n", and stores the floats into a 3*n array, called D, until it reaches the end of file (eof) ([3 inf])
D = D';                               % transposes D, turning it from an 3*n array to an n*3 array (this step is optional
fclose(fid);                          % closes the file
public/octavefaq.txt · Last modified: 2008/08/09 09:59 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki