MATLAB: Convert a text file to a matrix

convertmatrixstringstringstext file

Hello.
I have a text file with columns delimited by tabs. I have to convert it to a matrix. I have the following problems :
  • sometimes, there is two or more tabs in a row. It means that there is a blank cell.
  • sometime, two words are seperated by a point. When this happens, it doesn't mean that they are in different cells! Only the "tab" is a delimiter.
  • the collumns and lines are not always the same lenght.
Exemple of one line :
00982 FILE 2 Dupont Mary, May FEMALE 23.04.1999 0 89 11 23 2.01.2017 14:02:30 168 55 19.49 128 LABHUTC Yes 789 123 8 5 2 4 3
At first, I tried to use "strsplit(U)" but because of the problems I enonced, it doesn't work.
Oh and when I copy and paste the text file in excel, it gives me the right table.
Thank you very much in advance!

Best Answer

fun = @(s)regexp(s,'\t','split');
[fid,msg] = fopen('DML001_Carotid_Pressure_6.txt','rt');
assert(fid>=3,msg)
hdr = fun(fgetl(fid));
val = fun(fgetl(fid));
str = fgetl(fid);
while isempty(str)
str = fgetl(fid);
end
col = numel(fun(str));
out = {};
while ~feof(fid)
str = fgetl(fid);
tmp = str2double(fun(str));
tmp(end+1:col) = NaN;
out{end+1} = tmp;
end
fclose(fid);
mat = vertcat(out{:})
Giving:
mat =
567.000 187.000 61.050 70.000
215.000 288.000 69.350 69.120
217.000 289.000 71.520 NaN
219.000 291.000 NaN NaN
Bonus: convert the header to a structure:
hdr = regexprep(hdr,'\W','_')
val(end+1:numel(hdr)) = {''};
S = cell2struct(val(:),hdr(:),1);
Giving:
>> S.Surname
ans = Dupont
>> S.HEIGHT
ans = 168
>> S.System_ID
ans = 00982
etc.