1

I would like to use a specific return value from a function and pass it to another function, as a one-liner.

The problem is that I can't simply access the return value like someFunction(x, y, z){2} or [2] or (2), for example:

regexpi(str,'[a-z]+','match') % returns a cell array, i just need the first match.(btw, ^ doesn't work in matlab?)

if I would like to take the first child of the regexpi() and pass it to myfun(), what I want is:

myfun(regexpi(str,'[a-z]+','match')(1))

but I got an error instead:

Error: ()-indexing must appear last in an index expression.

Is there any workaround? thanks!

Quan Zhou
  • 65
  • 7
  • 1
    I suggest you just do it in two lines. Your code will be more readable that way. – Dan Dec 31 '14 at 06:48

1 Answers1

2

Unfortunately it cant be done in matlab, it is just not supported. The only way I know of, that is somehow elegant, is to create itemgetter (something ala itemgetter in python). For example,

itemgetter = @(r, idx) r{idx}


#now get first returned argument
itemgetter(regexpi(str,'[a-z]+','match'), 1)

For more info, and other possible ways have a look here.

Community
  • 1
  • 1
Marcin
  • 215,873
  • 14
  • 235
  • 294