help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Loading Files with mixed text and numbers


From: siko1056
Subject: Re: Loading Files with mixed text and numbers
Date: Sun, 9 Jul 2017 15:53:36 -0700 (PDT)

Fritz Sonnichsen wrote
> I did this all the time with Matlab--loaded files with the very common 
> form of records like this:
> 2017/07/07,   13:59:59,   022.2,   12.69
> 
> I found the matlab "textscan" absurdly complicated for this simple task, 
> generating nested table arrays with lots of associated book keeping of 
> braces, brackets etc. So I finally used matlab "readtable" which worked 
> fine.
> 
> When I tried this in Octave it became apparent that the readtable 
> function is not implemented. Using csvread leads me to invalid results 
> (it apparently is for numbers and does not support the date strings).
> 
> So - short or writing loops does Octave implement something similar to 
> readtable or some simple way to perform this task--loading comma 
> separated strings into a regular array?
> 
> Thanks
> Fritz

AFAIK, Octave does not support something similar to Matlabs table. To
overcome this, try to read for example a file with these lines:

2017/07/07,   13:59:56,   022.3,   12.66
2017/07/08,   13:59:57,   022.4,   12.67
2017/07/09,   13:59:58,   022.5,   12.68
2017/07/10,   13:59:59,   022.6,   12.69

Using a naive approach:

A = fileread ("log.txt");
A = strsplit (A, "\n");
for i = 1:length(A)
  B(i,:) = strtrim (strsplit (A{i}, ","));
endfor
# convert numerical values 3rd and 4th column of cell array of chars
B(:,[3, 4]) = cellfun(@str2num, B(:,[3, 4]), "UniformOutput", false);
fprintf("%s %s %f%f\n", B'{:})

Output:

2017/07/07 13:59:56 22.300000 12.660000
2017/07/08 13:59:57 22.400000 12.670000
2017/07/09 13:59:58 22.500000 12.680000
2017/07/10 13:59:59 22.600000 12.690000

HTH,
Kai



--
View this message in context: 
http://octave.1599824.n4.nabble.com/Loading-Files-with-mixed-text-and-numbers-tp4684044p4684046.html
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]