フィルターのクリア

How to print the data in the indicated manner with indicating numbers ?

2 ビュー (過去 30 日間)
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 9 月 30 日
コメント済み: Steven Lord 2020 年 9 月 30 日
I want to print the data on screen as shown below. I have a 1 x n double numeric vector. Let it contents be as follows.
n = [3.2,4.5,2.5,1.2,7.2];
using fprintf() statement i want to print it as follows:
1.3.2Hz 2.4.5Hz 3.2.5Hz 4.1.2Hz 5.7.2Hz
Is it possible to do so ?
As an example i have a cell array "k" of size 5 x 1. I was able to print this as:
2.4Hz 5.4Hz 3.5Hz 4.5Hz 1.8Hz
using the statement:
fprint("%s\t",string(k)+'Hz')
If anyone have idea on how to do this, share your thoughts.

採用された回答

Steven Lord
Steven Lord 2020 年 9 月 30 日
You're almost there. When you call string on a non-scalar double array, each element of the double array becomes an element of the string array.
n = [3.2,4.5,2.5,1.2,7.2];
s = string(n)
The + operator when given two compatible string arrays concatenates them element-wise.
part1 = string(1:numel(n)) + ". "
If one of the inputs to + is a string array and the other a compatible double array, the double is converted to a string to reduce this to a previously solved problem (two compatible string arrays.)
part2 = part1 + n + " Hz"
No fprintf needed, though if you're doing this as part of an assignment that requires it:
fprintf('%s\n', string(1:numel(n)) + ". " + n + " Hz")
  2 件のコメント
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 9 月 30 日
編集済み: Mahesh Babu Dhanekula 2020 年 9 月 30 日
It worked. I have one more doubt. suppose i have value of 2.093750000000000 and i want it to make 2.09375 by dividing it with some value. While doing so, matlab replaces this value as 2.0938. How can i overcome this problem ?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by