help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help with load command


From: niles
Subject: Re: help with load command
Date: Wed, 21 Sep 94 09:57:20 -0400

Here are two octave functions I use to read and write ASCII data:
 ( th stands for time history )
loadth.m:
---------

% Function to read in time history file with or without titles.
% header should be 1 if there is a one line header, else 0.
% nrows is optional but speeds things up alot.
function data = loadth(title, header, ncols, nrows)
fp = fopen(title, "r");
fseek(fp, 0, SEEK_END);
eof = ftell(fp);
frewind(fp);
if (header)
   headeris  =1
   for col=1:ncols
      name = fscanf(fp, "%s")
   endfor
endif

if (~exist('nrows'))
   eoh = ftell(fp); 
   pos = 0;
   nrows = 1;
   while( pos < eof-4 )
      for col=1:ncols
         a = fscanf(fp, "%lf");
      endfor
      nrows++;
      pos = ftell(fp); 
   endwhile
   nrows
   fseek(fp, eoh, SEEK_SET);
endif

data = zeros(nrows,ncols);
pos = 0;
row = 1;
for row=1:nrows
   for col=1:ncols
      data(row,col) = fscanf(fp, "%lf");
   endfor
endfor

endfunction

----------
saveth.m
----------
% Function to save in time history file without titles.
function saveth(filename, data, header)
shell_cmd(["rm -f " filename]);
fp = fopen(filename, "w");

format long;
[n m] = size(data);

for i = 1:n;
   for j = 1:m;
      fprintf(filename, "%lf ",data(i,j));
   end
   fprintf(filename,"\n");
end

fclose(fp);

endfunction

--------

Hope these help, John Eaton said he would implement this
file type as well as the Matlab binary format in a future
version of octave.  I sure you'll want to modify these, if
you have any questions, like why did you do this?, write
back.
                Rick Niles.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]