MATLAB Answers

Yue
2

Getting first element of a function output

Yue
さんによって質問されました 2012 年 5 月 18 日
Hi everyone,
I am trying to get the value of the first element of an output of a function. For example, The output of my function test is [a1, a2, a3] and I want to get the value of a1 only.
The way I do it is
a = test(input);
result = a(1);
I wonder if there is an easier way to do it. Is there something similar to
result = (test(input))(1);
in matlab?

  0 件のコメント

サインイン to comment.

2 件の回答

Andrei Bobrov
回答者: Andrei Bobrov
2012 年 5 月 18 日

no easier way
result = subsref(test(input),struct('type','()','subs',{{1}}))

  0 件のコメント

サインイン to comment.


Daniel Shub
回答者: Daniel Shub
2012 年 5 月 18 日

See this answer about what is missing from MATLAB.
If test is a function you wrote, you could change what it returns. You could also write a wrapper around test:
function a1 = test1(input)
a = test(input);
result = a(1);
end

  1 件のコメント

Jan
2012 年 5 月 18 日
In my opinion this method is easy, clear and efficient.
A modification of the Matlab syntax would be worse due to breaking the backward compatibility. +1

サインイン to comment.



Translated by