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

1 visualización (últimos 30 días)
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.

Respuesta aceptada

Steven Lord
Steven Lord el 30 de Sept. de 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 comentarios
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula el 30 de Sept. de 2020
Editada: Mahesh Babu Dhanekula el 30 de Sept. de 2020
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 ?

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by