Get numerical data from a string with multiple kinds of delimiters
3 views (last 30 days)
Show older comments
I have a text file (with very strange formatting) where each row is printed as
{2, 3} -> {-0.003201472132661235 + 0.00011724512961300188*I, 0.0024489343681961366 + 0.0012251936705803077*I,....}
This is very messy - let me clarify
The numbers 2 and 3 are indices. At point (2,3) we have a list of complex numbers where I = sqrt(-1). I'd like to translate this string into the row of a matrix formatted as
[2, 3, -0.00320+0.0001172i, 0.0024489+0.001225i, ...]
I have tried load / csvread but obviously not the right functions. Currently I am using fopen, reading each line with fgets, and then strtok repeatedly but this is going badly.
Do you have any suggestions? I would be most appreciative of your input.
Thanks!
PS these files can be North of 14MB so a real pain, too.
0 Comments
Accepted Answer
Walter Roberson
on 4 Dec 2012
T = fileread('YourFileNameHere.txt');
T = regexprep(T, '[{},]|->|\*I|(?<=[-+]) (?=\d)', '');
At this point, T is a single string delimited by newlines, in which all of the decoration is gone (including the commas and *I), so each line is just a list of numbers, such as
2 3 -0.003201472132661235 +0.00011724512961300188 0.0024489343681961366 +0.0012251936705803077 ...
If there are then a fixed number of items per line, you can
NumComplex = 2;
NumItem = 2 + 2 * NumComplex;
fmt = repmat('%f', 1, NumItem);
C = textscan(T, fmt, 'CombineOutput', 1);
and your matrix would then be
M = [C{1}(:,1) C{1}(:,2), complex(C{1}(:,3:2:end), C{1}(:,4:2:end))];
If the number of entries per line is variable, but there is a defined maximum, then you can use the above with NumComplex being the maximum number of complex pairs, and add 'MissingValue', NaN to the calling options.
If the number of entries per line is variable and there is no defined maximum, you cannot be using regular numeric arrays to store all the data at the same time. For efficient solutions to this scenario, please deposit more chocolate ;-)
0 Comments
More Answers (1)
See Also
Categories
Find more on Other Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!