Professor Diomar Cesar Lobao

Universidade Federal Fluminense-Volta Redonda, RJ, Brasil

Diomar Cesar


Dept. Ciências Exatas - Exact Science Dept.

Search

ReadWritePlot_Dio Matlab script

%========================================================================
%  File:  ReadWritePlot.m
%
%  Read a data file and plot it with symbols
%
%  By Prof. Diomar Cesar Lobao
%  UFF - Volta redonda RJ - Brazil
%  February 23, 2007
%------------------------------------------------------------------------
%  readColData  reads data from a file containing data in columns
%                that have text titles, and possibly other header text
% 
%   Synopsis:
%      [labels,x,y] = readColData(fname)
%      [labels,x,y] = readColData(fname,ncols)
%      [labels,x,y] = readColData(fname,ncols,nhead)
%      [labels,x,y] = readColData(fname,ncols,nhead,nlrows)
%    
%   Input:
%      fname  = name of the file containing the data (required)
%      ncols  = number of columns in the data file.  Default = 2.  A value
%               of ncols is required only if nlrows is also specified.
%      nhead  = number of lines of header information at the very top of
%               the file.  Header text is read and discarded.  Default = 0.
%               A value of nhead is required only if nlrows is also specified.
%      nlrows = number of rows of labels.  Default = 1
% 
%   Output:
%      labels  =  matrix of labels.  Each row of lables is a different
%                 label from the columns of data.  The number of columns
%                 in the labels matrix equals the length of the longest
%                 column heading in the data file.  More than one row of
%                 labels is allowed.  In this case the second row of column
%                 headings begins in row ncol+1 of labels.  The third row
%                 column headings begins in row 2*ncol+1 of labels, etc.
% 
%           NOTE:  Individual column headings must not contain blanks
% 
%      value_x = column vector of x values
%      value_y = matrix of y values.  y has length(x) rows and ncols columns
% 
%========================================================================
%  open file for input, including error handling
if input('\nWould you like to read the information from a file?(y,n)\n?','s')=='y'
		filename=input('Enter a file name.\n?','s');
		filename=strcat(filename,'.dat');
		fid=fopen(filename,'r');
          if fid < 0
             error(['Could not open ',filename,' for input']);
          end
   	readfromfile=1;
   end

%  read labels and x-y data
[labels,value_x,value_y] = readColData(filename,2,0);

plot(value_x,value_y(:,1),'-ko');

xlabel('Value of x');              %  add axis labels and plot title
ylabel('Value of y');
title('Your first graphic in Matlab...');
%
% End script of Read and Plot
Skip to content