How can I index a MATLAB array returned by a function without first assigning it to a local variable?

前端 未结 9 1648
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:02

For example, if I want to read the middle value from magic(5), I can do so like this:

M = magic(5);
value = M(3,3);

to get

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 05:50

    How do you feel about using undocumented features:

    >> builtin('_paren', magic(5), 3, 3)               %# M(3,3)
    ans =
        13
    

    or for cell arrays:

    >> builtin('_brace', num2cell(magic(5)), 3, 3)     %# C{3,3}
    ans =
        13
    

    Just like magic :)


    UPDATE:

    Bad news, the above hack doesn't work anymore in R2015b! That's fine, it was undocumented functionality and we cannot rely on it as a supported feature :)

    For those wondering where to find this type of thing, look in the folder fullfile(matlabroot,'bin','registry'). There's a bunch of XML files there that list all kinds of goodies. Be warned that calling some of these functions directly can easily crash your MATLAB session.

提交回复
热议问题