Can you make the output of this graph have multiple colors, like one timing bit is one color and the next one is another color.

5 vues (au cours des 30 derniers jours)
h = [1 1 0 1 0 1 0 1];
bitrate = 1;
n = 1000;
T = length(h)/bitrate;
N = n*length(h);
dt = T/N;
t = 0:dt:T;
x = zeros(1,length(t));
lastbit = 1;
for i=1:length(h)
if h(i)==1
x((i-1)*n+1:i*n) = -lastbit;
lastbit = -lastbit;
else x((i-1)*n+1:i*n) = lastbit;
end
end
d=plot(t,x);grid on;
title('Line code POLAR NRZ-I');
set(d,'LineWidth',2.5);
axis([0 length(h) -1.5 1.5]);
counter = 0;
lastbit = 1;
for i = 1:length(t)
if t(i)>counter
counter = counter + 1;
if x(i)~=lastbit
result(counter) = 1;
lastbit = -lastbit;
else result(counter) = 0;
end
end
end
disp(result);

Réponse acceptée

Image Analyst
Image Analyst le 9 Jan 2021
Modifié(e) : Image Analyst le 9 Jan 2021
To plot in different colors you have to specify a linespec in plot, like
d=plot(t,x, 'r-', 'LineWidth', 2); % Plot red line of width 2.
d=plot(t,x, 'b-', 'LineWidth', 3); % Plot blue line of width 3.
d=plot(t,x, '-', 'LineWidth', 2); % Plot line of width 2 in next default color;
Here's a full demo:
numLines = 10
lineColors = jet(numLines);
x = linspace(0, 2*pi, 1000);
period = pi;
legendStrings = cell(numLines, 1);
for k = 1 : numLines
y = sin(2*pi*x/(k * period));
plot(x, y, '-', 'Color', lineColors(k, :), 'LineWidth', 3);
hold on;
legendStrings{k} = sprintf('Curve #%d', k);
end
grid on;
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
legend(legendStrings, 'Location', 'southwest');
Also see attached m-file for a different demo.
  4 commentaires
Image Analyst
Image Analyst le 9 Jan 2021
Crish, here's another demo where I vary teh color with y value instead of x value:
clear all;
close all;
clc;
format long g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
x = linspace(0, 2*pi, 1000);
period = pi;
y = sin(2*pi*x/period);
numMarkers = numel(y);
markerColors = jet(numMarkers);
subplot(2, 1, 1);
for k = 1 : length(x)
plot(x(k), y(k), '.', 'Color', markerColors(k, :), 'MarkerSize', 30);
hold on;
end
grid on;
caption = sprintf('Line Color Varies Along X. Composed of %d Differently Colored Markers', numMarkers);
title(caption, 'FontSize', 20);
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
xlim([min(x), max(x)]);
% Maximize the figure window.
g = gcf;
g.WindowState = 'maximized';
% Do it again but instead of changing color as we move along the x axis,
% change it as we go along the y axis. So the marker color depends on the y value.
subplot(2, 1, 2);
numMarkers = numel(unique(y));
markerColors = jet(numMarkers);
miny = min(y);
maxy = max(y);
% Get indexes into the color map for each y value.
colorMapRows = round(rescale((y - miny) / (maxy - miny), 1, numMarkers));
for k = 1 : length(x)
thisMarkerColor = markerColors(colorMapRows(k), :);
plot(x(k), y(k), '.', 'Color', thisMarkerColor, 'MarkerSize', 30);
hold on;
end
grid on;
caption = sprintf('Line Color Varies With Y Value. Composed of %d Differently Colored Markers', numMarkers);
title(caption, 'FontSize', 20);
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
xlim([min(x), max(x)]);
fprintf('Done running %s.m.\n', mfilename);
I think you pretty much have all possibilities now.

Connectez-vous pour commenter.

Plus de réponses (2)

Crish
Crish le 13 Jan 2021
I tried your code but it's not really much working on a square wave. It's not making a vertical line.
  1 commentaire
Image Analyst
Image Analyst le 14 Jan 2021
Modifié(e) : Image Analyst le 14 Jan 2021
Correct. You do not have any data points there. If you really need lines between the data points, you can go to some more trouble to get them by drawing a line from each point to the prior point but the line will be all one color, not have a color gradient. I'm pretty sure you're capable of that but if you're not, attach your data.

Connectez-vous pour commenter.


Crish
Crish le 14 Jan 2021
It's fine now. I already solved the problem. Thank you for your ideas.

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by