How to resolve this error Error using fprintf Function is not defined for 'cell' inputs.

103 ビュー (過去 30 日間)
Sultan Al-Hammadi
Sultan Al-Hammadi 2018 年 4 月 7 日
コメント済み: Walter Roberson 2022 年 4 月 29 日
If I got a function like this:
function display(a,b,c)
%Where a & b are strings and c is an integer
fprintf('1: %s \n', a);
fprintf('2: %s \n', b);
fprintf('3: %d \n', c);
end
Also, I got an cell array such that
cell=
'Hi '
'Hey'
'232'
And I want to execute this script:
display(cell{1,1},{2,1},{3,1});
I will get that error
Error using fprintf Function is not defined for 'cell' inputs.
How to overcome this problem?
Thanks in advance

回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 4 月 7 日
display(cell{1,1}, cell{2,1}, cell{3,1});
Or you could use
display(cell{:});

KSSV
KSSV 2018 年 4 月 7 日
fprintf('%s\n',cell{:})
  2 件のコメント
Maksudul Alam
Maksudul Alam 2022 年 4 月 28 日
Sorry to say, but it doesn't work
Walter Roberson
Walter Roberson 2022 年 4 月 29 日
@Maksudul Alam what problem are you observing?
Note: this approach will not work directly if you have a cell array containing things that are compound structures such as struct or cell or table.
cell= {
'Hi '
'Hey'
'232'
}
cell = 3×1 cell array
{'Hi '} {'Hey'} {'232'}
fprintf('%s\n',cell{:})
Hi Hey 232

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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by