How to print the data in the indicated manner with indicating numbers ?
1 view (last 30 days)
Show older comments
Mahesh Babu Dhanekula
on 30 Sep 2020
Commented: Steven Lord
on 30 Sep 2020
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.
0 Comments
Accepted Answer
Steven Lord
on 30 Sep 2020
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 Comments
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!