MATLAB Answers

Can I keep the text of my delimiter or LineEnding when using readcell, readtable, etc.?

6 views (last 30 days)
I am using readcell to read in data from a log file. Because of the way it's logged, I need use '2020-' or '2019-' as my LineEnding, as follows:
data = readcell(filename, 'Delimiter','\t', 'LineEnding', {'2020-', '2019-'});
When I execute this line, it deletes the '2020-' text, but I need to keep the year in the data. Any way of doing this?

  4 Comments

Show 1 older comment
Adam Morrone
Adam Morrone on 30 Jun 2020
Yes, there is not a consistent \n break.
The records are not of fixed character length, but, as far as I can tell, each row has the same number of tab seperated columns.
I attached an example of the format that I'm working with.
dpb
dpb on 30 Jun 2020
Need to attach a section of a real file if people to have any chance to spearmint...
Unless there's a hidden/nonprinting character in the file preceding the date string, looks like easiest would be to read the whole file as a char() vector and use regular expression to locate the dates and then break on those marks. Could insert \n there to make easy...
Walter Roberson
Walter Roberson on 1 Jul 2020
readcell() converts each element independently if possible, leaving it as character vector if nothing else works.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Jul 2020
That is not possible using readtable() or readmatrix() or readcell() .
You can pre-process the file:
S = fileread(filename);
S = regexprep(S, '(2019-|2020-)', '\n$1');
tfile = tempname();
[fid, msg] = fopen(tfile, 'w');
if fid < 0
error('Filed to open temporary file "%s" because "%s"', tfile, msg);
end
fwrite(fid, S);
fclose(fid);
data = readcell(tfile, 'delimiter', '\t');
delete(tfile);

More Answers (1)

dpb
dpb on 1 Jul 2020
Edited: dpb on 1 Jul 2020
OK, try the following for starters...probably cleaner way to do the splits given the indices and direct index manipulation but that they're not fixed lengh records this is easy way to get the cellstr() array...
exp="\d{4}-\d{2}-\d{2}"; % regular expression for yyyy-mm-dd as digits
ix=regexp(s,exp); % find the beginning of each date string
ix=flip(ix(2:end)); % all except first, work from end of string forwards
for i=1:numel(ix),s=insertBefore(s,ix(i),newline);end % insert newline in front of each date
% show what get...
s=splitlines(s)
s =
4×1 cell array
{'2019-12-12 15:32:21→tab→seperated→data→is→here' }
{'2020-01-06 05:39:54→more→tab→seperations→blank→blank'}
{'2020-01-06 05:45:12→then→some→more→tabs→blank' }
{'2020-01-08 11:25:19→even→more→lovely→tabs→blank' }
>>
If is tab-delimited with fixed number of fields, then should be pretty straightforward to then parse into table or timetable or whatever form wanted...if they didn't have that much discipline you may have another step or three to deal with.

  0 Comments

Sign in to comment.


Translated by