Read values from .txt file to vectors in Matlab -


i have data in text file, contains varying amount of columns @ each row in text file. data have @ following format:

2

21 2623

707 40 1

after there's 3 numbers in row data remains @ same structure until end of file. want process data have 3 vectors x, y , z, contains values rows have 3 numbers, that:

x = 707

y = 40

z = 1

thanks lot!

assuming number of rows ignore not constant , want programmatically rather using import wizard, you'll first need figure out how many rows there ignore.

one method use fgetl, goes through file line-by-line. once have number of headers can read in data function dlmread or textscan.

for example:

fid = fopen(myfilepath, 'r'); % open data file reading  ncoldesired = 3;  ii = 0; % search first row of "good" data while ~feof(fid) % loop stop if reaches end of file     tline = fgetl(fid);     temparray = str2num(tline); % convert string array of doubles, use regex      if length(temparray) == ncoldesired         % found first row of data        nheaders = ii;        fclose(fid);        break     end      ii = ii + 1; end  % error catching if exist('nheaders', 'var') == 0 && feof(fid)      fclose(fid);     error('end of file reached, no data found'); end  mydata = dlmread(myfilepath, ' ', nheaders, 0); % reason dlmread uses zero-based indexing unlike else in matlab 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -