필터 지우기
필터 지우기

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일
편집: 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