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

2 次查看(过去 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
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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by