フィルターのクリア

Spli cell array directly at the function output

2 ビュー (過去 30 日間)
frlby
frlby 2024 年 5 月 14 日
回答済み: Stephen23 2024 年 5 月 15 日
Hi,
I have a function returning two 1*n cell arrays as output, which I want to append to two existing cell arrays (variable1 and variable2). However, I don't want to end up with nested cells arrays, so my code looks like this:
variable1 = { 'var11', 'var12' };
variable2 = { 'var21', 'var22' };
[ output1, output2 ] = myFunc();
variable1 = [ variable1, output1{:} ];
variable2 = [ variable2, output2{:} ];
In this case, is it possible to append the outputs to variable1 and variable2 and exploding the cells in only one line instead of 3. So something like this (which obviously does not work but it is just to be sure I am clear enough):
variable1 = { 'var11', 'var12' };
variable2 = { 'var21', 'var22' };
[ [ variable1, output1{:} ], [ variable2, output2{:} ] ] = myFunc();
Thanks for your help.
  3 件のコメント
Matt J
Matt J 2024 年 5 月 14 日
編集済み: Matt J 2024 年 5 月 14 日
Avoiding nested cells does not require that you "explode" the cells using a comma-separated-list. You could have just done this, and gotten the same result.
variable1 = { 'var11', 'var12' };
variable2 = { 'var21', 'var22' };
[ output1, output2 ] = myFunc();
variable1 = [ variable1, output1];
variable2 = [ variable2, output2];
This assumes output1 and output2 are not theselves nested cell arrays.
frlby
frlby 2024 年 5 月 15 日
I unfortunately do not know n in advance. I could certainly modify the function but I need it to return a cell array.
You are fully right that I did not have to explode the cell array.

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

回答 (1 件)

Stephen23
Stephen23 2024 年 5 月 15 日
If
  • you can modify the function to return another output argument
  • the size of the returned cell arrays is only known when they are returned
then here is one possibility:
v1 = {'var11', 'var12'};
v2 = {'var21', 'var22'};
[n,v1(end+1:end+n),v2(end+1:end+n)] = myfun()
n = 2
v1 = 1x4 cell array
{'var11'} {'var12'} {'next'} {'hello'}
v2 = 1x4 cell array
{'var21'} {'var22'} {'text'} {'world'}
function [n,C1,C2] = myfun()
C1 = {'next', 'hello'};
C2 = {'text', 'world'};
n = numel(C1);
end

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by