Displaying Value beside graph

10 次查看(过去 30 天)
Mark Rodger
Mark Rodger 2018-12-8
回答: sMall 2020-1-28
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

回答(2 个)

Koundinya
Koundinya 2018-12-14
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

sMall
sMall 2020-1-28
I do not want to use title, legend or axis label. What would be the other ways?

类别

Help CenterFile Exchange 中查找有关 Simulation, Tuning, and Visualization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by