how to read a text file that contains a sinewave form in one column,then plot it on matlab?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1143200/image.png)
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)
0 Commenti
Risposta accettata
William Rose
il 30 Set 2022
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Annotations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!