function plot_from_file(datafile) // // Make a simple x-y plot based on values read from a datafile. // We assume that the datafile has four columns of floating-point // values. // // In this sample, we plot column 4 versus column 2, but different // columns can be chosen by changing the "x_col" and "y_col" values below. // // Arguments: // datafile (input) name of file containing data // // MWR 4/17/2007 // set verbose = 1 to see lots of diagnostics verbose = 1; // which columns should we plot? x_col = 4; y_col = 2; // open the datafile (quit if we can't) fid = mopen(datafile, 'r'); if (fid == -1) error('cannot open datafile'); end // loop over all lines in the file, reading them one at a time num_lines = 0; done_yet = 0; while (done_yet == 0) // try to read the next line ... [num_read, val(1), val(2), val(3), val(4)] = mfscanf(fid, "%f %f %f %f"); if (num_read <= 0) break; end if (verbose > 0) fprintf(1, 'num_lines %3d num_read %4d \n', num_lines, num_read); end if (num_read ~= 4) break; end // okay, that line contained valid data. Store in arrays num_lines = num_lines + 1; x_array(num_lines) = val(x_col); y_array(num_lines) = val(y_col); end // now, make the plot plot(x_array, y_array, 'o'); xtitle("Simple graph from data in a file", "length of pendulum", ... "period of pendulum"); // close the datafile mclose(fid); endfunction