How to print the string value using fprintf

Hi,
a = (aa bb cc dd)
for ion = 1: 1: length (a)
and some lines of code
then later i have some functionality
---------------
p = a (ion);
Results if i use
fprintf ( "% 10s" , p);
error: fprintf: wrong type argument 'cell'
suppose if i use
disp (p)
output
{
[1,1] = aa
}
I just want the 'aa' as the output using fprintf only
How do i do that? Kindly help.

 採用された回答

Rik
Rik 2020 年 4 月 22 日

0 投票

The error message explains what is going on: you are trying to use a cell as input to fprintf. fprintf will not 'unpack' that for you, so you'll have to do it on your own:
fprintf ( "% 10s" , p{1});

8 件のコメント

Ganesh Kini
Ganesh Kini 2020 年 4 月 22 日
編集済み: Ganesh Kini 2020 年 4 月 22 日
Thanks!
But do you have any other way / method of representing it?
Rik
Rik 2020 年 4 月 22 日
What do you mean?
Ganesh Kini
Ganesh Kini 2020 年 4 月 22 日
Instead of p {1} do you have any other alternative ?
Rik
Rik 2020 年 4 月 22 日
I don't understand why you are asking this. Is something not working? Your question showed that p is actually a scalar cell with some contents. If you use curly braces you can access those contents, which is what p{1} does.
Because it doesn't make sense to search for an alternative if this code works, I am guessing this code is not working for you. So what is going wrong?
Ganesh Kini
Ganesh Kini 2020 年 4 月 22 日
The code is working and I am getting the output as expected.
Just for improving my knowledge i wanted to know if there are other ways of representing it.
Rik
Rik 2020 年 4 月 22 日
For the p{1} part you don't have a choice. The only possibility is to use the curly braces sooner:
p = a{ion};
Ganesh Kini
Ganesh Kini 2020 年 4 月 22 日
Okay, Thanks! :)
Stephen23
Stephen23 2020 年 4 月 23 日
編集済み: Stephen23 2020 年 4 月 23 日
"...i wanted to know if there are other ways of representing it."
There is no "representing" occuring, it is simply the MATLAB syntax for accessing the contents of a cell array:

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2020 年 4 月 22 日

編集済み:

2020 年 4 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by