MatLab - How to read a text file separated by ';', with different number of columns -


this question has answer here:

i'm trying read .txt file ';' delimited date in 'header' , diferent columns after 'header'. i'm using quotes header because it's more parameter line.

so, .txt (the other lines have same number of columns):

15/07/2013;66;157  ddd;3;1;0;1;1;1;-0.565 ddd;8;2;0;2;1;1;-0.345  ddd;9;3;2;3;1;2;-0.643  ddd;8;1;3;5;1;3;-0.025  ddd;8;1;0;9;1;4;-0.411  ddd;15;1;5;4;1;5;-0.09  ddd;12;1;0;5;1;6;-0.445  ddd;13;1;0;7;1;7;-0.064 

i want read , create matrix, contains each data in 1 cell, like:

matrix =  [15/07/2013 66 157  ddd 3 1 0 1 1 1 -0,565  ddd 8 2 0 2 1 1 -0,345  ddd 9 3 2 3 1 2 -0,643 ...] 

i've tried textscan, cvsread, textread , nothing works!

thanks in advance!

edit: actually, found way faster code this!

from past experience, matlab not strings , numbers in same matrix, forced use cell.

you can relatively simple paring.

fid = fopen('temp.txt','r'); %# open file reading count = 1; content = {}; while ~feof(fid)     line = strtrim(fgets(fid)); %# read line line        parts = regexp(line,';','split');        = 1:numel(parts)              temp = regexp(parts{i},'-?[0-9]*\.?[0-9]*(i|j)?','match');              if numel(temp) >= 1 && strcmpi(temp{1},parts{i})                   parts{i} = str2double(parts{i}) ;              end        end        content{count} = parts;     count = count + 1; end fclose(fid);  numrows  = size(content,2)-1; whole = cell(numrows,8); = 1:numrows     j = 1:8        whole{i,j} = content{i+1}{j};     end end content = {content{1},whole}; 

update

i added stuff put single cell array, of data outside of header. not know if wand header in 8 column array, if here code that

numrows  = size(content,2); whole = cell(numrows,8); = 1:numrows     j = 1:min([size(content{i},2),8])        whole{i,j} = content{i}{j};     end end whole 

Comments