I want a label for each plot but my code to do so is not working....any tips?

1 view (last 30 days)
%*************************************************************************
%this function plots the first amplitude and arrival arrival time of the
%acoustic wave to each transducer in one event
%*************************************************************************
function frstarrv()
fid = fopen('event_1'); %open the event folder
File_names = dir('*.csv'); %attain every waveform in the event folder
numFiles = length(File_names);
% numFiles = 2;
% csvFiles = cell(1,numFiles);
incr = [ 1,2,3,4,5,6,7,8,9,10];
% leg = zeros(1,numFiles);
for i = 1:numFiles %call graphData() for each waveform
file = File_names(i).name;
% newStr = extractBetween(file,"_","_"); %get transducer name
% legend("Transducer" + " " + newStr)
% subplot(2,1,i);
hold on
plus = incr(i);
% newStr = extractBetween(file(i),"_","_"); %get transducer name
[pksa(i,1),loca(i,1)]= graphData(file,plus);
% hold on
% legend("Transducer" + " " + newStr);
end
newStr = extractBetween(file,"_","_"); %get transducer name
legend("Transducer" + " " + newStr, 'location', 'northwest')
end
%*************************************************************************
%%This fucntion will graph waveforms and their hilbert transform from one
%%waveform .csv file
%%must add labeling for first peak and this function needs an input
%**************************************************************************
function [p,l] = graphData(file,plus)
% newStr = extractBetween(file,"_","_"); %get transducer name
% legend("Transducer" + " " + newStr);
table = readtable(file); %read the data into a table
xr = table2array(table); %convert the table into an array(matrix)
x = xr(:,1);
y = xr(:,2);
[up] = envelope(y); %attain the hilbert envelope fo the amplitude
m = [up];
% M = find(up == max(m)); %find the max amplitude of the hilbert transform envelope
[pks,loc] = findpeaks(m);% find the peaks of the hilbert envelope
p = pks(1,1);
l = loc(1,1);
%I want to offset each waveform vertically so that all waveforms are on one
%figure
% incr = rand();
%
% if incr < .5
% incr = 2;
% else
% incr = -3;
% end
%I want to limit the range of of peak locations when plotting
% newStr = extractBetween(file,"_","_"); %get transducer name
% legend("Transducer" + " " + newStr);
plot( x,y + plus , x, up + plus ,'-p','MarkerIndices',l,...
'MarkerFaceColor','blue',...
'MarkerSize',5) %plot time vs. amplitude vs. hilbert and mark max amplitude of hilbert
xlabel('Time (seconds)');
% ylabel('Volts');
title("Peak in Hilbert Transform of Signal")
% hold off
% legend("Transducer" + " " + newStr);
end

Answers (1)

infinity
infinity on 21 Jun 2019
Hello,
Here is my suggestion.
You could create a new variable name "text" like
text = sprintf('transducer %d', i)
where "i" is the value of corresponding file, for example, 1, 2, 3, 4, ...
Then, you use the option 'DisplayName' in your plot function like
plot(...., 'DisplayName',text)
It will give you a figure with legend correspondig to your data.
Hope that could help you.

Tags

Community Treasure Hunt

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

Start Hunting!