Asked by David Dominguez
on 28 Jun 2013

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

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;

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.

Answer by Kelly Kearney
on 28 Jun 2013

Accepted answer

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.

David Dominguez
on 1 Jul 2013

Thank you Kelly, I appreciate your answer. That's a much cleaner program:

function IMUdata %% Import data datafile = importdata('2Axis1Profile.log',',');

%% Initializations clc clf 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

%% Draw Baseline basePlot = round(axis1);

%% Plot Axes hold on plot(time,axis1) plot(time, basePlot, 'r') end

I'll need to figure out how to deal with the spikes now. I get this: <http://www.damado.com/NASA/matlab/round.PNG>

Kelly Kearney
on 1 Jul 2013

Looks like your data doesn't quite fit the conditon of "data values are within 0.5 of their base values" that would be necessary in order to use round. Here's a quick trick to match the data points with the nearest value in a set:

vals = [0 1 5 10 -10]; idx = interp1(vals, 1:length(vals), axis1, 'nearest', 'extrap'); basePlot = vals(idx);

Opportunities for recent engineering grads.

## 0 Comments