I'm trying to get input for sine wave and then plot it while printing values in command windows. But i'm getting more values than i expected to get.
Is there anything wrong in this code?
clc;
clear;
N = input('Length=') ;
fs = input('Sampling Frequency=') ;
f1 = input('Frequency=') ;
ts = 1/fs ;
t = -(N/(2*fs)):ts:(N/(2*fs)) ;
y = sin(2*pi*f1*t) ;
plot(t,y);
fprintf('Time=%4.1f Amp=%6.1f \n' , t , y) ;

 채택된 답변

KL
KL 2017년 10월 10일

0 개 추천

You're creating vectors and so when you use them (t,y) in fprintf, you get multiple values.

댓글 수: 5

Gombo Khorloo
Gombo Khorloo 2017년 10월 10일
I mean in this case fprintf must print values of t between -(N/2*fs) to (N/2*fs) but it is printing more values after this as you can on the screenshot.Thanks for the fast reply.
KL
KL 2017년 10월 10일
편집: KL 2017년 10월 10일
change the last line to something like
fprintf('Time=%4.4f Amp=%6.4f \n' , t , y) ;
%4.4f means 4 digits after the decimal place.
Gombo Khorloo
Gombo Khorloo 2017년 10월 10일
No,no i mean let's think N=1024,fs=200,f1=1.So in this case fprintf must print value of t from -2,6 upto +2,6 but as i can see from command window fprintf starts printing unexpected values after +2,6.This is problem that i'm looking for answer.I don't understand this.Sorry for taking your time.
The fprintf function is vectorized, but perhaps not in the way you think. When you have multiple format operators in the format specification and you pass multiple data inputs into fprintf, it does not take the first elements of each of the data inputs to fill in the format operators for the first line printed, the second elements of each for the second line, etc. It iterates through the columns of the first data input, then moves to the columns of the second data input, etc.
x = 1:5;
y = 11:15;
fprintf('%d %d\n', x, y)
You might expect this to display:
1 11
2 12
3 13
4 14
5 15
Instead it displays:
1 2
3 4
5 11
12 13
14 15
To get what you expected, stack x and y on top of one another as per the Write Tabular Data to Text File example on the documentation page for fprintf.
fprintf('%d %d\n', [x; y])
Gombo Khorloo
Gombo Khorloo 2017년 10월 10일
Thank you very much.Helped so much :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

질문:

2017년 10월 10일

댓글:

2017년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by