how to read a text file that contains a sinewave form in one column,then plot it on matlab?

조회 수: 12 (최근 30일)
hi guys,
I would like to plot a signal that is saved in text file where I know the sampling frequency.
I tried this code but did not work properly.
Fs=10e3; %sample frequency
t=0:1/Fs:1; %period
fileID = fopen('sine50Hz.txt','w');
fprintf(fileID,'%6.4f\n',t);
sine50=fscanf(fileID, '%6.4f');
plot(t, sine50)
this is what I get
the code for generating this sinewave is:
fsig50=50; %signal frequency
sine50=100000*sin(2*pi*fsig50*t);
filename='sine50Hz.txt';
csvwrite(filename,fix(sine50)');
%%delete("sine1Hz");
plot(t,sine50)

채택된 답변

William Rose
William Rose 2022년 9월 30일
It seems that you have opened the file for writing ('w') which erases the exisitng file, if it exists. You write the time vector to the file. Then you attempt to read from the file with fscanf(). You would have to close it first, the open it for reading ('r'). I'm not sure exactly what you are trying to do. If the text file already exists, with 2 columns of data (time and the sine wave), then do
data=load('sine50Hz.txt','-ascii');
plot(data(:,1),data(:,2))
If the text file is just 1 column of data, the sine wave without the times, the do the following:
sine50=load('sine50Hz.txt','-ascii');
Fs=10e3; dt=1/Fs;
t=(0:length(sine50)-1)*dt;
plot(t,sine50)
Try it. Good luck.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by