function [b, columnwidth]=loadascii(filename,commentsign,nofile); % % [matrix, columnwidth] = loadascii(filename,commentsign,nofile) % % Function loads an asciifile "filename" % automatically recognizing number and text clumns. % "commentsign" is an optional parameter which excludes % that part of the lines after "commentsign" till the end % of each line. % "nofile" is an optional parameter. If it is given, "filename" is % interpreted as a variable including the source string. % % In the rowvector "columnwidth", % the conversion parameters are given. % "columnwidth" includes the same number of elements % as the number of columns in "filename" % % The j. element in "columnwidth" means: % % columnwidth(j)=6 : The j. column in "filename" % includes numbers % columnwidth(j)<0 : The j. column in "filename" % includes an ascii text and is represented % by abs(columnwidth(j)) columns in "matrix". % If the lengths of the column texts are % different in the rows, the shorter texts % are elongated by spaces. % % "columnwidth" is helpfull for PRINTASCII. % Numbers of type byte can not be distinguished to 1-ascii sign columns. % To print a 1-ascii sign column, change the corresponding value in % "colmnwidth" from 6 to -1 manually before using PRINTASCII. % % US 11/02/98 % modified: US 21/01/04 % modified: US 07/05/08 (commentsign parameter) % modified: US 04/12/08 (nofile option) % if nargin==3 daten=filename; if size(daten,2)~=1 daten=daten'; end else if ~isempty(dir(filename)) f=fopen(setstr(filename),'r'); daten=fread(f,'char'); fclose(f); else daten=[]; end end if ~isempty(daten) % remove CR (windows files) daten=daten(daten~=13); % replace TABs by blanks daten(find(daten==9))=32; % remove comments if nargin>1 if ~isempty(commentsign) comm_pos=find(daten==commentsign); eol_pos=find(daten==10); rem=[]; for j=1:length(comm_pos) h=eol_pos-comm_pos(j); h(h<0)=max(h)+1; if ~isempty(h) rem=[rem comm_pos(j):eol_pos(min(find(h==min(h))))-1]; else rem=[rem comm_pos(j):length(daten)]; end end daten=daten(setdiff(1:length(daten),rem)); end end while daten(length(daten)-1:length(daten))==[10;10] | daten(length(daten)-1:length(daten))==[32;10] while daten(length(daten)-1:length(daten))==[10;10] daten=daten(1:length(daten)-1); end while daten(length(daten)-1:length(daten))==[32;10] daten=[daten(1:length(daten)-2); 10]; end end daten=[daten(find(~((daten(2:length(daten))==10) & (daten(1:length(daten)-1)==10)))) ; daten(length(daten))]; daten=[daten(find(~((daten(2:length(daten))==32) & (daten(1:length(daten)-1)==32)))) ; daten(length(daten))]; daten(find(daten==32))=9; daten=[daten(1) ; daten(find(~((daten(2:length(daten))==9) & (daten(1:length(daten)-1)==10)))+1)]; daten=[daten(find(~((daten(2:length(daten))==10) & (daten(1:length(daten)-1)==9)))) ; daten(length(daten))]; anfangleerzeilen=0; while daten(1)==9 | daten(1)==10 if daten(1)==10 anfangleerzeilen=anfangleerzeilen+1; end daten=daten(2:length(daten)); end leerezeilenpos=find(((daten(2:length(daten))==10) & (daten(1:length(daten)-1)==10))); daten=[daten(find(~((daten(2:length(daten))==10) & (daten(1:length(daten)-1)==10)))) ; daten(length(daten))]; spalten=find(daten==9); zeilen=find(daten==10); zeilenanzahl=size(zeilen,1); zeilensprungweite=sum(spaltenzeilenanzahl*zeilensprungweite pruef=pruef(1:zeilenanzahl*zeilensprungweite); b=[];columnwidth=[]; end pruef(zeilensprungweite:zeilensprungweite:length(pruef)); pruef=find((pruef(zeilensprungweite:zeilensprungweite:length(pruef))==zeilen)==0); if ~(isempty(pruef)) pruef=pruef(1)+sum(leerezeilenposlength(daten) daten(length(daten)+1:max(max(rohspalte)))=32*ones(max(max(rohspalte))-length(daten),1); end rohspalte(:)=daten(rohspalte); for j2=1:zeilenanzahl if ende(j2)-anfang(j2)