[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Improved aload.m
From: |
Eyal Doron |
Subject: |
Improved aload.m |
Date: |
Tue, 8 Aug 1995 11:56:52 +0100 (MET DST) |
Hi Guys,
You can try the following. This is a version of aload.m which
includes more error checking. Only partially tested, though.
Eyal
-----------------------Cut here-----------------------------
function [X]=aload(filename,nr,nc)
% function [X]=aload(filename,nr,nc) loads a flat ASCII file into X.
% nr - number of rows (-1 = all (default))
% nc - number of columns (-1 = all (default))
% Also accepts a 1x2 matrix [nr,nc].
% Eyal Doron, August 1995 (address@hidden)
X=[];
if (~exist(filename))
error("File not found")
end
if nargin<2
nr=-1; nc=-1;
elseif nargin==2
if length(nr)==2
nc=nr(2); nr=nr(1);
else
nc=-1;
end
end
if nr<1 | nc<1
Cmd=["wc -lw ", filename, " | awk '", '{printf "WC=[%g,%g];",$1,$2}',"'"];
eval(shell_cmd(Cmd,1));
if ~all(WC)
error([filename," contains zero rows/columns!"])
end
ncc=WC(2)/WC(1); if nc<1, nc=ncc; end
if ncc~=ceil(ncc)
error([filename," does not have a constant number of columns!"])
end
if ncc<nc
error([filename," has too few columns!"])
end
if nr<1, nr=WC(1); end
end
Tname=octave_tmp_file_name;
fid=fopen(Tname,"w");
fprintf(fid,"# name: localX\n# type: matrix\n");
fprintf(fid,"# rows: %g\n#columns: %g\n",nr,nc);
fclose(fid);
if nargin<2
shell_cmd(["/bin/cat ", filename, " >> ", Tname]);
else
shell_cmd(["/usr/bin/head -n ", num2str(nr), " ", filename, ...
" >> ", Tname]);
end
eval(["load -force -ascii ",Tname]);
shell_cmd(["/bin/rm -f ", Tname]);
X=localX;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Improved aload.m,
Eyal Doron <=