Basic file I/O in Matlab -


i trying read particular textfile matlab, , store float values matlab matrix. have found few different ways it, none work quite way want. think problem formatting of textfile. here first few lines of file:

**k = 1, j = 1 37.9072 37.9072 37.872 37.9072 37.9072 37.5572 37.9072 37.9072 37.9172 37.9072 37.962 37.9552 37.9072 37.9222 37.9072 37.9322 37.9072 37.9332 37.9072 ** k = 2, j = 1 34.9249 34.9249 34.9349 34.9249 34.9679 34.9249 34.9249 34.9249 34.2439 34.9249 34.9249 34.9249 34.9249 34.9459 34.9249 34.9549 34.9249 34.6749 34.9889 **k = 3, j = 1 37.94501 37.94401 37.94501 37.94501 37.99501 37.96501 37.94501 37.94501 37.94101 37.93301 37.94501 37.94501 37.94501 37.94501 37.90501 37.94501 37.90001 37.94501 37.99801 

the issues having that: 1) each number not separated tab, , instead separated multiple spaces, , 2) first row of each line after '**' contains 7 columns of data, , subsequent rows contain 6 columns of data. able read lines want using tline = fgetl(fid), how extract floating values in tline can assign them matlab vector?

ideally, have each block of data separated '**', i/o code reads matlab vector. example, first block k = 1, j = 1, have vector:

vec1 = [37.9072 37.9072 37.9072 37.9072 37.9072 ...]

and second block k = 2, j = 1, have:

vec = [37.9429 37.9429 37.9429 37.9429 37.9429 37.9429 ...]

each vector, vec of size [1 19] (ie. 19 data points in each block of data). below attempt @ problem (which not correct):

fid = fopen('temp2017-01-01.txt');  m = 1;  while ischar(tline)  if(tline(1) == '*') %to skip lines start '*'     tline = fgetl(fid); elseif(length(tline) > 112) %to line containing 7 columns of data  vec(m, :) = sscanf( tline( 1:end ), '%f,%f,%f,%f,%f,%f,%f' )  else %to lines containing 6 columns of data      vec(m, :) = sscarf(tline(1:end), '%f,%f,%f,%f,%f,%f,%*s' )  end  m = m + 1;  end  fclose(fid); 

any advice appreciated. thanks,

edit: still cannot work using textscan hoki suggested (thanks btw). error messages are: 1) improper assignment rectangular empty matrix, , 2) cannot support cell arrays containing cell arrays or objects. also, assignment m = cell2mat(g) returns like:

nan nan nan nan nan nan nan nan 37.9828800000000 nan nan nan nan nan nan nan 37.9828800000000

i forgot make clear between each float in sample textfile above, there 8 spaces. when use textscan returns cell array of strings. each string like:

'37.9072    37.9072    37.9072       ...' 

i extract each of these individual entries vector or matrix. upon reading errors get, 1 explanation cell2mat works non-string entries. further advice appreciated.

thanks,

the textscan function friend (read doc more info). using :

fid = fopen('temp2017-01-01.txt');  vec = zeros( 0,19 ) ; while ~feof(fid)     l0 = fgetl(fid) ; %// nothing (or extract k , j if need them)      %// read many '%f' numbers can (19 in case)     m = textscan( fid , '%f' , 'delimiter',' ' ) ;      vec(end+1,:) = cell2mat( m ) ; %// convert them matrix , add global result end  fclose(fid) 

mlint complain matrix vec growing @ each iteration unless know in advance number of block going read have no choice. go it.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -