How can I get this code to print the "Non-Reversal Filtered Data" graph from a txt file? I get the "Removed No-Reversal Cycles" currently.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1296150/image.jpeg)
% Read data from text file
data = readmatrix('Aero.txt');
% Find the indices of local extrema
[~, locs] = findpeaks(data);
[~, mins] = findpeaks(-data);
% Check if the first extremum is a maximum or a minimum
if locs(1) < mins(1)
isMax = true;
else
isMax = false;
end
% Remove no-reversal cycles
if isMax
for i = 1:length(locs)-2
if locs(i) > mins(i) && locs(i+1) < mins(i+1)
continue;
elseif locs(i) < mins(i) && locs(i+1) > mins(i+1)
continue;
else
data(locs(i):mins(i)) = NaN;
end
end
else
for i = 1:length(mins)-1
if mins(i) > locs(i) && mins(i+1) < locs(i+1)
continue;
elseif mins(i) < locs(i) && mins(i+1) > locs(i+1)
continue;
else
data(mins(i):locs(i)) = NaN;
end
end
end
% Remove NaN values
data(isnan(data)) = [];
% Write the modified data to a new text file
dlmwrite('flight_recorded.txt', data, 'delimiter', '\n');
% Read data from text file
data1 = readmatrix('flight_recorded.txt');
% Plot the data
plot(data1);
% Add labels and title
xlabel('Cycles');
ylabel('Load/Stress Level');
title('Removed No-Reversal Cycles');
0 Commenti
Risposte (1)
Nirupama Nagarathinam
il 7 Mar 2023
Modificato: Nirupama Nagarathinam
il 7 Mar 2023
You will require the "x" axis data as well to replicate the plot. The data that you have attcahed contains only the "y" axis data. I have assumed some "x" values for the data you have provide and the corresponding code and figure are as follows:
%reading the data that conatins only Load level
y=readmatrix('Aero.txt');
% creating x array of the same size of y
x=ones(size(y));
% for the values of y that are repeating consecutively, I am assigning the
% same x values
for i=2:length(x)
if y(i)==y(i-1)
x(i)=x(i-1);
else
x(i)=x(i-1)+1;
end
end
%finding the peaks of Load level and their index value
[ylocs,locs]=findpeaks(y);
[ymins,mins]=findpeaks(-y);
%The index values (locs and mins) that we got have to be reassigned to the
%corrsponding x values
for j=1:length(locs)
locs(j)=x(locs(j));
end
for k=1:length(mins)
mins(k)=x(mins(k));
end
%ploting the curve and the peaks on the same figure
plot(x,y)
hold on
plot(locs,ylocs,'*r',mins,-ymins,'*r',"MarkerSize",3)
%figure title and labels
xlabel('Cycles');
ylabel('Load/Stress Level');
title('Removed No-Reversal Cycles');
The Cycles (x) values I have assumed might be different from the original, Otherwise the plot is very much similar to the one you are trying to replicate.
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!