Can't figure out why plot is not giving the correct graph with either switch or if-elseif code
Mostrar comentarios más antiguos
I am plotting gyro data from a 3-axis table test to measure the variation from a known standard rate. The blue is the gyro data at 0, 1, 5, 10 and -10 degrees per second. I want to plot a red line that is exactly 0,1,5,10,-10 and have it appear only in the appropriate section.
Here's my code:
function main1
%%Import data
close all
datafile = importdata('2Axis1Profile.log',',');
%%Initializations
clc
clf
close all
data = datafile(1:end,6); %Choose all rows in column 6 - gyros in ? axis
sample = 1:length(data); %Assign a time for each data point
axis1 = data./100; % Convert back to real values
time = sample/5; % Convert time to seconds from 5Hz
% basePlot = 1:length(data);
basePlot = zeros(1,length(data));
%%Draw Baseline
for i = 1:length(data)
v = axis1(i);
switch v
case (-0.5 <= v && v <= 0.49)
basePlot(i) = 0;
case (0.5 <= v && v <= 1.5)
basePlot(i) = 1;
case (4.5 <= v && v <= 5.5)
basePlot(i) = 5;
case (9.5 <= v && v <= 11.5)
basePlot(i) = 10;
case (-11.5 < v)&& (v < -9.5)
basePlot(i) = -10;
end
end
%%Plot Axes
hold on
plot(time,axis1)
plot(time, basePlot, 'r')
end
I have also tried the elseif version:
for i = 1:length(data)
v = axis1(i);
if (-0.5 <= v && v <= 0.49)
basePlot(i) = 0;
elseif (0.5 <= v && v <= 1.5)
basePlot(i) = 1;
elseif (4.5 <= v && v <= 5.5)
basePlot(i) = 5;
elseif (9.5 <= v && v <= 11.5)
basePlot(i) = 10;
elseif (-11.5 <= v && v <= -9.5)
basePlot(i) = -10;
end
end
Which gives this graph: <http://www.damado.com/NASA/matlab/elseif.PNG> This graph does have the red line I want, but it also has all that red in between.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!