-1

Consider:

textscan('5,6,7', '%s', 'Delimiter',','){1}{1:3}
ans = 55
ans = 66
ans = 77

Excellent! Now I want those three answers in different variables, or some structure I can do things with.

So I try:

X = textscan('55,66,77', '%s', 'Delimiter',','){1}{1:3}
X = 55

Only one value.

So I try:

X = [textscan('55,66,77', '%s', 'Delimiter',','){1}{1:3}] '
X = 556677

It's made it into one string.

So I try:

X Y Z = textscan('55,66,77', '%s', 'Delimiter',','){1}{1:3}
  ^
syntax error

There must be some way to handle multiple answers ... but how?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Why do you need them as separate variables? – sco1 Mar 22 '17 at 12:21
  • `[a,b,c]` is how you get multiple variables as output in MATLAB, as you can see in almost all documentation pages of MATLAB. However, `texscan` does not support multiple separated variables as output, as you can clearly see in the documentation. – Ander Biguri Mar 22 '17 at 12:22
  • 3
    Use a single variable containing an array with three values. [Using dynamic variables is bad](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170). – Adriaan Mar 22 '17 at 12:24
  • Parsing with `'%f'` or `'%d'` instead of `'%s'` would probably help. You want numbers, not strings. – sco1 Mar 22 '17 at 12:31

1 Answers1

2

This question originally was asked mistakely about MATLAB, while it's about Octave.

The answer to the question, as @excaza wrote in the comments is:

[a, b, c] = textscan('5,6,7', '%d%d%d', 'Delimiter', ','){:};

The first answer, that I wrote for MATLAB use is this:

You need to define three separate outputs inside textscan:

a = textscan('5,6,7', '%s%s%s', 'Delimiter', ',');
[b, c, d] = a{:}

b =
    '5'
c =
    '6'
d =
    '7'

If you want the output as numbers and not as text, you can use %d:

a = textscan('5,6,7', '%d%d%d', 'Delimiter', ',');
[b, c, d] = a{:}

b =
      5
c =
      6
d =
      7
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adiel
  • 3,071
  • 15
  • 21
  • 1
    Works, but how'd you extend this when the OP has a thousand, or a million numbers? (Not that that's the question, but for the sake of generallity) – Adriaan Mar 22 '17 at 12:34
  • 2
    First, I answered the question... But you right, I also agree (and voted already..) with your and others comments on the question about handling with just one variable. But sometimes, for some reasons, I do need the separate variables,( if it's up to 3 or 4, not thousands...), so it sounds reasonable enough to me... – Adiel Mar 22 '17 at 12:39
  • Muh, you could just use `b(1)` for the first number, `b(2)` for the second etc. There are your "separate" variables. Read in cleanly, not cluttering your workspace and easy to use. – Adriaan Mar 22 '17 at 12:41
  • Yes, this is the reason that I'm agree with you. But if I had a shared big code, for example, that has already some parameters or variables, and I want to work with it, so it's sometimes is needed. – Adiel Mar 22 '17 at 12:50
  • @Adiel thanks for the reply, I'm using Octave not Matlab and it turns out it handles this differently. Thanks for being nice. ans = { [1,1] = 5 } ans= ... etc. Thanks, I'm done with this question and place tho –  Mar 22 '17 at 12:59
  • OK, I'm not familiar with Octave. If my answer does not help for that purpose I will delete it. Can someone confirm it? – Adiel Mar 22 '17 at 13:02
  • @Adiel There is no difference. Octave does support indexing into intermediate arrays, so you can do `[a, b, c] = textscan('5,6,7', '%d%d%d', 'Delimiter', ','){:};`, which provides the desired result. He's probably getting the different answer because he's still parsing it with `%s` and not with a numeric flag. – sco1 Mar 22 '17 at 13:12
  • @excaza See my edit. Now I think that question can be defined and tagged as Octave\Matlab. what do you think? – Adiel Mar 22 '17 at 13:23
  • It's not exactly a profound question. There are plenty of similar MATLAB questions already, like [this one](http://stackoverflow.com/questions/16126438/how-do-i-consume-multiple-outputs-of-textscan-function-in-a-single-line) – sco1 Mar 22 '17 at 13:27