image thumbnail

plotmydataR14

by

 

09 Nov 2004 (Updated )

Read and plot .xls files by column quickly.

plotmydata.m
% plotmydata (Version 2.01R14)
%
% Written by LeRoy W. Alaways   Version 1.0 Summer 2000 at Fastball Inc.
% lwalaways@mac.com             Matlab R11
%                               Version 2.0 October 19, 2003
%                               Matlab R12/R13  6.5.0
%                               Matlab R14  7.0.1  November 9, 2004
%
% This program is designed to quickly plot Excel ".xls" files, tab
% delimited files ".txt", old Macintosh cricket-graph files with ".txt"
% extension and Matlab generated data.
%
% Maximum number of columns in data set = 25.
%
% To execute double click on plotmydata.fig
%
% GUI has been tested on a Mac iBook G3 12-inch with no problems.
%
% Known Bug or Nuisance: If editable text boxes do not accept data changes
% (especially after changing the order of the curve fit on a Mac or
% UNIX system) single click on the figure menu bar.  This should change the
% status of the editable text boxes back to being editable.
%
% Another Bug or Nuisance: Does not read file in properly if first column
% is characters (alpha) only.
%
% Commands:
%
% BUTTONS:
% Load:     This button allows you to load tab-delimited .txt and .xls files.
%           The files should look like the following
%           (file does not have to be in same folder as m-file):
%
%           .xls file
%           Bob     Bill    Mary    Joe
%           10      20      20      10
%           15      16      17      16
%           17      18      19      18
%
%           .xls file can have "holes" in it.  For example the following
%           files are okay as well.
%           Bob     Bill    Mary    Joe
%           10      20      20      10
%           15              17      16
%           17      18              18
% or
%           Bob     Bill    Mary    Joe
%           10      20      20      10
%           15      16      bad     16
%           delete  18      19
%
%           Tab-delimited .txt files MUST NOT have holes and can look like
%           the following:
%
%           Bob     Bill    Mary    Joe
%           10      20      20      10
%           15      16      17      16
%           17      18      19      18
% or
%           *
%           Bob     Bill    Mary    Joe
%           10      20      20      10
%           15      16      17      16
%           17      18      19      18
%
%           A '*' in the first row was used in the old mac cricket-graph
%           files.
%
%
% Data:     Matlab generated can be used and loaded with this
%           button. The variables "labels" and "data" MUST be used.
%           Both variable MUST be global with labels being a M-column
%           vector and data being N x M.   Limitation of M <= 26
%           Example:
%
%           global data labels
%           labels = ['Bob '; 'Bill'; 'Mary'; 'Joe '];
%           data = [10 20 20 10; 15 16 17 16; 17 18 19 18];
%
%
% Plot:     Plots the selected variables with the selected color, marker
%           and line scheme.  If "copy figure" below is selected, plot will
%           generate a new plot axis.
%
%
% Hold:     Toggles plot hold on/off.  Note you can load data, plot, hold,
%           load new data and then do an overlay plot. Use Matlab built-
%           in "Legend" command under the "Insert" pull-down menu for a
%           legend.
%
%
% Clear:    Clears plot only, NOT data.
%
%
% Grid:     Toggle Grid on/off.
%
%
%
% Copy Figure:  Once you get the plot to be how you want it.  Use Copy Figure
%               to copy figure to a new window for plotting, printing or
%               copying without all the buttons of plotmydata.
%
%
% Washout NaN's:    Excel data may have "holes" in it.  See above under
%                   load.  Matlab will treat these as "NaN" (Not a Number)
%                   and will not plot them.  Matlab will also give NaN as
%                   the mean or std of the data set.  Washout NaN's will
%                   remove all NaN's in the selected x- and y-data sets.
%                   
%                   This is a toggle and uses data with NaN's when toggled
%                   off.  Note that you know you have NaN's in the data
%                   when NaN is given as the mean/std in one or both data
%                   sets.  The mean/std may change for the data set not
%                   containing a NaN if selected.
%
% MENU ITEMS:
% Numbers:          Changes Font and Size of the Axes Numbers.
% Title:            Changes Font and Size of the Title.
% Xlabel:           Changes Font and Size of the Xlabel.
% Ylabel:           Changes Font and Size of the Ylabel.
%
% OTHER COMMANDS:
%
% All other Matlab figure options are available.
%
% E-mail author at lwalaways@mac.com or ic3d2@yahoo.com with comments,
% bugs, suggestions.  A 3-D version is in the works and an updated 2-D
% version with pull down menus instead of radio buttons is in works as
% well.
%
% Author assumes no responsibility of unchecked plots or figures. Author
% requests that changes to code and copies to code be acknowledged.
%
% Last change; 25 October 2003
% Last change; 7 December 2003 -- Added path feature allowing one to open data
%    file in any folder and returns to same folder if opening second file.
% Last change; 9 Novembeer 2004 -- removed a few features to make R14
%    compatible.


function plotmydata(goflag);

warning off MATLAB:polyfit:RepeatedPointsOrRescale
warning off MATLAB:deblank:NonStringInput

global xflag yflag cflag mflag lflag curvehandle poly currentplot columns
global datafile datapath data firstline labels xdata ydata

if goflag == 'load'
   
   if isempty(datapath)
       current_dir = pwd;
       filterspec = strcat(current_dir,'*.txt;*.xls');
       [datafile,datapath] = uigetfile(filterspec,'Choose a data file');
   else
       filterspec = strcat(datapath,'*.txt;*.xls');
       [datafile,datapath] = uigetfile(filterspec,'Choose a data file');
   end
   
   %open and read input file
   
   fid = fopen(strcat(datapath,datafile),'r');
   datafile_length = length(datafile);
   file_ext = datafile(datafile_length-2:datafile_length); % file extension
   if fid < 0;
      disp('File not found')
   elseif file_ext == 'txt'
      firstline = fgetl(fid); %first line - headers
      labels = strtok(firstline);
	  if labels == '*' %skip the first line and do again
	  	firstline = fgetl(fid);
      	labels = strtok(firstline);
	  end
      line = fgetl(fid); %second line -first line of data
      data = [];
      index = 1;
      while line ~= -1
         A = sscanf(line,'%f');
         A = [index; A]; %adding index to data
         data = [data; A'];
         line = fgetl(fid);
         index = index + 1;
      end
      fclose(fid);

      set(findobj('tag','ckbx_value(1)'),'string','Index');
      set(findobj('tag','ckbx_value(101)'),'string','Index');
      set(findobj('tag','ckbx_value(2)'),'string',labels);
      set(findobj('tag','ckbx_value(102)'),'string',labels);
   
      [rows,columns] = size(data);

      strstart = length(labels) + 2;
      strend = strstart + 1;
      
      labels = strvcat('Index',labels);
      
      for i = 2:1:columns-2
         while((~isequal('	',firstline(strend))) | strend == (length(firstline)))
            strend = strend + 1;
         end
         nextlabel = firstline(strstart:strend-1);
         labels = strvcat(labels,nextlabel);
         strstart = strend + 1;
         strend = strend + 2;
         row1 = strcat('ckbx_value(',num2str(1+i),')');
   	     row100 = strcat('ckbx_value(',num2str(101+i),')');
   	     set(findobj('tag',row1),'string',nextlabel);
   	     set(findobj('tag',row100),'string',nextlabel);
      end
      labels = strvcat(labels, firstline(strstart:length(firstline)));
      row1 = strcat('ckbx_value(',num2str(columns),')');
      row100 = strcat('ckbx_value(',num2str(columns+100),')');
      set(findobj('tag',row1),'string',firstline(strstart:length(firstline)));
      set(findobj('tag',row100),'string',firstline(strstart:length(firstline)));
   
      for i = columns+1:1:26
         row1 = strcat('ckbx_value(',num2str(i),')');
         row100 = strcat('ckbx_value(',num2str(i+100),')');
	     set(findobj('tag',row1),'string','----');
         set(findobj('tag',row100),'string','----');
	  end
   
      plotmydatacheck(1);
      plotmydatacheck(102);
      plotmydatacheck(204);
      plotmydatacheck(301);
      plotmydatacheck(401);
      curvehandle = [];
   
   elseif file_ext == 'xls'
      xlsdatafile = strcat(datapath,datafile);
      [data,labels] = xlsread(xlsdatafile);
      
      cell_labels = labels(1,:)'; %labels
      labels = char(cell_labels);
      labels = strvcat('Index',labels);
      
      [rows, columns] = size(data); %data
      index = 1;
      for i = 2:1:rows
          index = [index; i];
      end
      data = [index data];
      
      usedata;
   else
       %do nothing
   end
   
elseif goflag == 'plot'
      
   if cflag == 201;
      color = 'm';
   elseif cflag == 202;
      color = 'r';
   elseif cflag == 203;
      color = 'g';
   elseif cflag == 204;
      color = 'b';
   elseif cflag == 205
      color = 'k';
   elseif cflag == 206
      color = 'y';
  else
      color = 'c';
   end
   
   if mflag == 301;
      mark = 'o';
   elseif mflag == 302;
      mark = 'x';
   elseif mflag == 303;
      mark = '+';
   elseif mflag == 304;
      mark = '*';
   elseif mflag == 305;
      mark = 's';
  elseif mflag == 306;
      mark = 'd';
  else
      mark = '';
   end
   
   if lflag == 401;
      line = '';
   elseif lflag == 402;
      line = '-';
   elseif lflag == 403;
      line = '--';
  elseif lflag == 404;
      line = ':';
  else
      line = '-.';
   end
   
   symbol = strcat(mark,color,line);
   
   nango = get(findobj('tag','togglenan'),'value'); %washout NaN's
   xdata = data(:,xflag); ydata = data(:,yflag-100);
   if nango == 0
       %do nothing
   else %Washout xdata and ydata
       %check data(:,xflag) first
       xnans = isnan(xdata);
       sumxnan = sum(xnans);
       if sumxnan == 0
           %do nothing
       else
           %washout x
           [rowsx,columnsx] = size(xdata);
           i = 1;
           while sumxnan > 0
               if xnans(i)
                   newx = xdata(i+1:length(xdata));
                   xdata(length(xdata)) = [];
                   xdata(i:i+length(newx)-1) = newx;
                   newy = ydata(i+1:length(ydata));
                   ydata(length(ydata)) = [];
                   ydata(i:i+length(newy)-1) = newy;
                   i = i - 1;
                   xnans = isnan(xdata);
                   sumxnan = sum(xnans);
               end
               i = i + 1;
           end
       end
       ynans = isnan(ydata);
       sumynan = sum(ynans);
       if sumynan == 0
           %do nothing
       else
           %washout y
           [rowsy,columnsy] = size(ydata);
           i = 1;
           while sumynan > 0
               if ynans(i)
                   newy = ydata(i+1:length(ydata));
                   ydata(length(ydata)) = [];
                   ydata(i:i+length(newy)-1) = newy;
                   newx = xdata(i+1:length(xdata));
                   xdata(length(xdata)) = [];
                   xdata(i:i+length(newx)-1) = newx;
                   i = i - 1;
                   ynans = isnan(ydata);
                   sumynan = sum(ynans);
               end
               i = i + 1;
           end
       end
   end
   
   plot(xdata,ydata,symbol);
   labels_width = size(labels); labels_width = labels_width(2);
   xtitle = labels(xflag,1:labels_width); ytitle = labels(yflag-100,1:labels_width);
   xlabel(xtitle); ylabel(ytitle); title(datafile);
   set(findobj('tag','etxt_title'),'string',datafile);
   set(findobj('tag','etxt_xlabel'),'string',xtitle);
   set(findobj('tag','etxt_ylabel'),'string',ytitle);
   
   set(gca,'position',[0.48668 0.136499 0.418033 0.762611]);
   if get(findobj('tag','push_grid'),'userdata'); grid on; end
   if get(findobj('tag','push_hold'),'value');
      hold on;
   else
      if ~isempty(curvehandle)
         curvehandle = [];
      end
   end       
   
   set(findobj('tag','etxt_mean'),'string',num2str(mean(ydata)))
   set(findobj('tag','etxt_std'),'string',num2str(std(ydata)))
   set(findobj('tag','xtxt_mean'),'string',num2str(mean(xdata)))
   set(findobj('tag','xtxt_std'),'string',num2str(std(xdata)))
   set(gca,'ButtonDownFcn','current = get(gca,''currentpoint'');set(findobj(''tag'',''etxt_currentx''),''string'',num2str(current(1,1)));set(findobj(''tag'',''etxt_currenty''),''string'',num2str(current(1,2)))');
   
elseif goflag == 'data'
    datafile = 'my data';
    usedata;
elseif goflag == 'eval'
    %do nothing
else
    %do nothing
end

function usedata();

global xflag yflag cflag mflag lflag curvehandle
global datafile data firstline labels

[rows,columns] = size(data);

      for i = 1:1:columns
         nextlabel = labels(i,:);
         row1 = strcat('ckbx_value(',num2str(i),')');
   	     row100 = strcat('ckbx_value(',num2str(100+i),')');
   	     set(findobj('tag',row1),'string',nextlabel);
   	     set(findobj('tag',row100),'string',nextlabel);
      end

      for i = columns+1:1:26
         row1 = strcat('ckbx_value(',num2str(i),')');
         row100 = strcat('ckbx_value(',num2str(i+100),')');
	     set(findobj('tag',row1),'string','----');
         set(findobj('tag',row100),'string','----');
	  end

      plotmydatacheck(1);
      plotmydatacheck(102);
      plotmydatacheck(204);
      plotmydatacheck(301);
      plotmydatacheck(401);
      curvehandle = [];

Contact us