1

I have four streams of complex numbers in four columns of a text file

I have to read them into a MATLAB matrix in the same format.

I tried the following, but it does not work

fid =  fopen('~/<path-to-file>/<fileName>.txt','r');
out = textscan(fid, '(%f,%f) \b\t(%f,%f) \b\t(%f,%f) \b\t(%f,%f) \n','CollectOutput',1);
tapWeights = [out{1} + 1i*out{2} out{3} + 1i*out{4} out{5} + 1i*out{6} out{7} + 1i*out{8}];
fclose(fid);

Note that help options for textscan lists \b\t as the white space delimiter.

The following is the output I get,

>> out

out = 

    [1x8 double]

>> out{1}

ans =

     1     0   NaN   NaN   NaN   NaN   NaN   NaN

What am I missing here?

Naveen
  • 458
  • 1
  • 10
  • 29
  • try `dlmread` - you can set the delimiter you wish to use. – Lui Jan 26 '16 at 20:30
  • 1
    You don't even need to specify the whitespace in your format string – `textscan(fid, '(%f,%f) (%f,%f) (%f,%f) ')` is sufficient. You might also consider using the `'CollectOutput'` option. – horchler Jan 26 '16 at 20:37
  • Thanks for pointing out. The extra (%f,%f) is a typo, but that is not the issue. Can you elaborate on the 'CollectOutput'? A full answer with working code would be very helpful. – Naveen Jan 26 '16 at 20:40
  • If `textscan(fid, '(%f,%f) (%f,%f) (%f,%f) ')` doesn't work for you then your data is not what you claim. There may be other characters or maybe you don't have new lines. Hard to say much without the real file (or an example one that exhibit the same issue). Read the documentation for `'CollectOutput'` and try it once you get the rest working to see if it's something you want. – horchler Jan 26 '16 at 20:43
  • I've tried all that and it doesn't work. You can copy the numbers given above into a text file to verify if it works. See my edit. – Naveen Jan 26 '16 at 21:12
  • 1
    FWIW, I tried @horchler s solution, and it works if the input text file has exactly the same format as specified. The way it is typed in the question, all the numbers are in one line, and that's not the same as your description. Maybe this is why it isn't working? – akamath Jan 26 '16 at 22:14
  • @Naveen: I did exactly that and my `textscan` worked fine. As I said, is t'd to say much more without the real file (or an example one that exhibit the same issue). Copy-pasting changes many things. I suggest that you provide a link to an example file. – horchler Jan 27 '16 at 00:24
  • @horchler: Yes, I just noticed that there is a problem copying input in highlighted text to a txt file. Sorry for the trouble. I have added the entire file in the edit. Please note that the text file has 4 columns of complex numbers now. – Naveen Jan 27 '16 at 15:49
  • Be very careful: mixing space-delimited arrays and complex numbers can be dangerous: `[3 + 2i]` *could* be `[3, +2i]` as well. I suggest using a comma between array elements to make your intentions clear to the user. – Andras Deak -- Слава Україні Jan 27 '16 at 16:21
  • @Naveen: I just downloaded your file and ran your code (only changed path and removed `'CollectOutput'` because the `tapWeights` wasn't updated). It worked perfectly fine in R2015b on OS X. So did `textscan(fid, '(%f,%f) (%f,%f) (%f,%f) (%f,%f) ')`. – horchler Jan 27 '16 at 16:22
  • 1
    @AndrasDeak: I'm not sure what you're talking about. The only thing in the file are doubles, which are then assembled into complex values after they're loaded in Matlab. If you're referring to the line after `textscan`, that seems to be valid Matlab code, though I would remove the spaces around `+` just for clarity. – horchler Jan 27 '16 at 16:24
  • @Naveen: The only whitespace characters in your file are spaces. There don't seem to be any `'\b'` (backspace) or `'\t'` (tab) characters, so adding those to your format string is unnecessary and may be confusing `textscan` if you're using an old version of Matlab. – horchler Jan 27 '16 at 16:33
  • @horchler the latter is exactly what I meant. – Andras Deak -- Слава Україні Jan 27 '16 at 16:43
  • @horchler This is strange indeed. I have R2015a on Linux Ubuntu based OS and both the formats of textscan(..) discussed above does not work for me. out{2} to out{8} or out(2) to out(8),whichever applies, are always empty or NaN. – Naveen Jan 27 '16 at 16:43

1 Answers1

0

Unless I missed something, a simple regexp, str2double, cellfun and reshape does the trick (assuming that the structure of the matrix is known in order to reshape it.

s='(1.00000000,0.00000000) (1.00000000,0.00000000) (-0.00000004,1.00000000) (-0.00000004,-1.00000000) (0.66912299,0.74315202) (0.66912299,0.74315202) (-0.74315202,0.66912293) (0.74315202,-0.66912305) (-0.10454900,0.99452001) (-0.10454900,0.99452001) (-0.99452001,-0.10454904) (0.99452001,0.10454895) (-0.80903500,0.58776098) (-0.80903500,0.58776098) (-0.58776093,-0.80903500) (0.58776104,0.80903500) (-0.97813898,-0.20795099) (-0.97813898,-0.20795099) (0.20795104,-0.97813898) (-0.20795095,0.97813898) (-0.49995700,-0.86605000) (-0.49995700,-0.86605000) (0.86605000,-0.49995697) (-0.86605000,0.49995703) (0.30907401,-0.95103800) (0.30907401,-0.95103800) (0.95103800,0.30907404) (-0.95103800,-0.30907398) (0.91357303,-0.40667400) (0.91357303,-0.40667400) (0.40667397,0.91357303) (-0.40667403,-0.91357303) (0.30893400,0.95108300)';
a=reshape(cellfun(@(x) str2double(x{1})+str2double(x{2})*1j,regexp(s,'[(]([^,]*),([^)]*)[)]','tokens')),3,[])'
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Alexander Kemp
  • 202
  • 1
  • 10
  • Yes, you're missing something important – as the OP says in the question the data comes from a text file, not a generic string. Also, `regexp` is much slower than `textscan` and `str2double` is a better choice than `str2num` (see documentation). – horchler Jan 27 '16 at 00:21
  • @horchler. You are right about str2double, corrected that. regexp may be slower, but I prefer it due to it's flexibility as long as performance does not matter. – Alexander Kemp Jan 27 '16 at 08:11
  • 2
    "a simple `regexp`, `str2double`, `cellfun` and `reshape` does the trick`": for an appropriately chosen meaning of "simple";) – Andras Deak -- Слава Україні Jan 27 '16 at 16:05
  • Unless my eyes are fooling me, that (at least to me) string of commands isn't perceived as "simple". – rayryeng Jan 27 '16 at 16:21