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;
%% Plot Axes hold on plot(time,axis1) plot(time, basePlot, 'r') end
This gives the following graph: <http://www.damado.com/NASA/matlab/switch.PNG>
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;
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.
Switch/case is definitely not the best choice for this (pretty sure it doesn't work with inequalities at all). The idea behind your second example should work, assuming that all of the values in your axis1 dataset actually fall in the given ranges. Are there missing values (NaNs) in there though? Wherever a v(i) doesn't match your prescribed ranges, the corresponding basePlot(i) is set to 0, which would explain why your second plot jumps between the expected ranges and the axis so much.
If all the base values are whole numbers, and all the data values within 0.5 of their base values, then
basePlot = round(axis1);
will accomplish what you want more cleanly. Of course, if there are a ton of NaNs in that dataset, you'll probably want to eliminate those before plotting, or you may get a very broken line.