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. We plot column 4 versus column 2. % % Arguments: % datafile (input) name of file containing data % % MWR 4/11/2001 % set debug = 1 to see lots of diagnostics debug = 1; % open the datafile (quit if we can't) fid = fopen(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 ... line = fgets(fid); if (line == -1) break; end [data_array, num_read] = sscanf(line, '%f %f %f %f '); if (debug > 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) = data_array(2); y_array(num_lines) = data_array(4); end % now, make the plot plot(x_array, y_array, 'o'); title('Simple graph from data in a file');