The issue with the legend colors not matching the plotted lines in your MATLAB code could be due to the legend not associating each entry with the corresponding plot. Here is a sample code which you can utilize to modify your code:
% Sample data
time = linspace(0, 1, 100); % Normalized time from 0 to 1
data1 = sin(2 * pi * time); % Sample data for X
data2 = cos(2 * pi * time); % Sample data for Y
data3 = sin(2 * pi * time + pi/4); % Sample data for Z
% Colors for each plot
colors = {'b', 'k', 'y'};
labels = {'Data X', 'Data Y', 'Data Z'};
% Create figure
figure;
hold on;
% Plot each dataset and store plot handles
plotHandles = gobjects(1, 3); % Preallocate plot handles
plotHandles(1) = plot(time, data1, 'Color', colors{1}, 'LineWidth', 2);
plotHandles(2) = plot(time, data2, 'Color', colors{2}, 'LineWidth', 2);
plotHandles(3) = plot(time, data3, 'Color', colors{3}, 'LineWidth', 2);
% Add labels
xlabel('Normalized Time');
ylabel('Amplitude');
title('Sample Data Plot');
% Create legend using plot handles
legend(plotHandles, labels, 'Location', 'northwest');
% Display the plot
hold off;
After running this code, the below figure is obtained as the output. In this figure, the legends are matched with the respective plots correctly.
For more information on legends, please follow the below link:
I hope this helps!