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

5 次查看(过去 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 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by