Displaying Value beside graph
6 views (last 30 days)
Show older comments
I've typed up a code that displays the time and freqeuecny domain of a signal from an arduino in real time. I would like to display the live value of the freqeuecny domain (FFT) beside the graph/same window or in separate window. Any suggestions?
clear all
ar = serial('COM6','BaudRate',9600);
Fs = 50;
try
fopen(ar);
catch err
fclose(instrfind);
error('Make sure you select the correct COM Port where the Arduino is connected.');
end
Tmax = 20; % Total time for data collection (s)
Ts = 0.001; % Sampling time (s), it correspond to 1000Hz
i = 0;
data = 0;
t = 0;
x_label_names = {'Time (s)', 'Frequency (Hz)'};
subplot(2,1,1)
h = animatedline;
xlabel('Time (s)')
ylabel('Amplitude (mV)')
tic % Start time
while toc <= Tmax
i = i + 1;
% Fill the data matrix with the data read from arduino
data(i) = fscanf(ar,'%d');
% If matlab read the data faster than the sampling rate set in arduino, force sampling to be the same as the
% sampling time set in matpab code, If matlab is reading slower, nothing can be done.
t(i) = toc;
if i > 1
T = toc - t(i-1);
while T < Ts
T = toc - t(i-1);
end
end
t(i) = toc;
for k = 1:length(t(i))
addpoints(h,t(i),data(i));
[timeLogs,voltLogs] = getpoints(h);
nfft = length(voltLogs); %length of domnain signal
nfft2 = 2.^nextpow2(nfft); %lenght of signal in power of 2
ff = fft(voltLogs,nfft2);
fff = ff(1:nfft2/2);
xfft = Fs.*(0:nfft2/2-1)/nfft2
subplot(2,1,2)
plot(xfft,abs(fff));
xlabel('Frequency (Hz)')
ylabel('Amplitude (mV)')
drawnow;
end
end
![Live Plot.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/198578/Live%20Plot.jpeg)
0 Comments
Answers (2)
Koundinya
on 14 Dec 2018
The value can be displayed in the figure title, legend or the axis label. You could format the variable to be displayed into a string using sprintf.
for k = 1:length(t(i))
addpoints(h,t(i),data(i));
[timeLogs,voltLogs] = getpoints(h);
nfft = length(voltLogs); %length of domnain signal
nfft2 = 2.^nextpow2(nfft); %lenght of signal in power of 2
ff = fft(voltLogs,nfft2);
fff = ff(1:nfft2/2);
xfft = Fs.*(0:nfft2/2-1)/nfft2
subplot(2,1,2)
plot(xfft,abs(fff));
% Value to be displayed
value_string=sprintf('Frequency(Hz) = %d',xfft);
% Display the updated value in x-axis label
xlabel(value_string);
% Or Display the updated value in the title
title(value_string);
%xlabel('Frequency (Hz)')
ylabel('Amplitude (mV)')
drawnow;
end
0 Comments
sMall
on 28 Jan 2020
I do not want to use title, legend or axis label. What would be the other ways?
0 Comments
See Also
Categories
Find more on Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!