How to index an output without creating intermediate variable?

12 ビュー (過去 30 日間)
arnold
arnold 2020 年 11 月 19 日
コメント済み: arnold 2020 年 11 月 23 日
Hi,
what keeps frustrating me about Matlab - or my lack of Matlab skills that is..
How do I do this more nicely
tmp = get(gca, 'YLim');
set(gca, 'YLim', [0, tmp(2)]);
clear tmp;
% I would imagine something like this:
set(gca, 'YLim', [0, get(gca, 'YLim'){2}]);
It's not about this specific example, it's about how to avoid having to create a temporary variable in order to index the output. I could come up with a ton of other examples that have crossed my projects over the years.
here's another example which is obviously dumbed down. The idx will not be hardcoded obviously
CellWithStuff = {'one', 'two', 'three', 'four'};
tmp = contains(CellWithStuff, 'o');
OutputIWant = tmp(1);
% what I'm looking for is replacing
contains(CellWithStuff, 'o')[1] % accessing the first entry of the output somehow simpler...
I'm not a professional developer, so maybe there is an obvious solution to something like this.
I'd appreciate any suggestions!
Arnold
  2 件のコメント
James Tursa
James Tursa 2020 年 11 月 19 日
MATLAB doesn't support this capability natively, so the only thing you can do is live with it or use klunky workaround code.
arnold
arnold 2020 年 11 月 23 日
hm.... not what I wanted to hear, at least I'm not entirely incapable of searching the web for answers then (I never found any).

サインインしてコメントする。

回答 (1 件)

David Hill
David Hill 2020 年 11 月 19 日
編集済み: David Hill 2020 年 11 月 19 日
CellWithStuff{find(contains(CellWithStuff,'o'),1)};
CellWithStuff(find(contains(CellWithStuff,'o'),1));%depending on what you want

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by