textscan - Matlab:Reading textfile into matrix -


i want read data text file in matlab. file describes surface consists of 2 parts: 1)a header information on x,y dimensions in pixels , nm etc. 2)matrix height values (512x512)

when try read matrix first end single column vector instead of matrix c = textscan(id,'%f','headerlines',18,'delimiter','\t') file contains " " seperated values , lines seperated rows of matrix. works when manually delete header , read a=dlmread(path,'\t',[0 0 511 511]);

i want obtain same 512x512 matrix textscan can later parse header - how can that?

if not strictly bound textscan, may use fscanf instead (see docs). this:

fid = fopen('filename.txt');  % here have skip (or parse) header lines % , determine number of rows/columns in data matrix  = fscanf(fid, '%g', [ncolumns nrows]); fclose(fid);  % transpose matches % orientation of file = a'; 

update1

this code might not elegant, parse header fileinfo structure may use data later in code.

% sample file header % % # file format = ascii % # created spip 6.2.8.0 2014-08-15 20:41 % # original file: d:\... % # x-pixels = 512 % # y-pixels = 512 % # x-length = 319375 % # y-length = 319375 % # x-offset = 0 % # y-offset = 0 % # z-unit = [nm] % # scanspeed = 638.75 % # forcecurve = 0 % # voidpixels =76 % # description =62:confocal height image  date: 2013-08-20t13:36 user: unknown % # start of data: matrix  fid = fopen('text.txt','r');  fileinfo  = []; tmpstring = '';   % initializing s while isempty(strfind(tmpstring,'start of data: matrix'))      % read string file     tmpstring = fgetl(fid);     % , split left , right parts according '=' position     [kev,val] = strtok( tmpstring(3:end) , ':=' );      % remove spaces key name , make valid variable name in matlab     keyname = genvarname( strrep(strrep(kev,'-',''),' ','') );      % check if key value number     [x,ok]    = str2num(strtrim(val(2:end)));      % if value number, use otherwise leave trimmed original string     if ok         fileinfo.(keyname) = x;     else         fileinfo.(keyname) = strtrim(val(2:end));     end  end  % can read data = fscanf(fid, '%g', [fileinfo.xpixels fileinfo.ypixels]); fclose(fid);  % transpose matches % orientation of file %a = a'; 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -