How to insert a complex root in a text using fprintf?

Hi, I'm new to matlab and could use some help regarding a pretty basic commando. This is my hypotethical problem:
*>> a = [1 0 4]
a =
1 0 4
>> b = roots(a)
b =
0 + 2.0000i
0 - 2.0000i
>> c = num2str(b)
c =
0+2i 0-2i
>> d1 = c(1,:)
d1 =
0+2i
>> d2 = c(2,:)
d2 =
0-2i
>> fprintf('One root is: %g',d1) One root is: 48One root is: 43One root is: 50One root is: 105>>*
I would like the fprintf commando to print: One root is: 0+2i But for some reason I get a very weird print :/ Thanks in advance.

回答 (1 件)

Titus Edelhofer
Titus Edelhofer 2011 年 11 月 25 日

2 投票

Hi,
you will need to print real and imaginary part seperately:
fprintf('One root is %g %+g i\n', real(d1), imag(d1));
Titus

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

質問済み:

2011 年 11 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by