help tidying my code
Show older comments
Hi everyone, I am creating a matlab script to process an accelerometer's orientation data from a text file and graph the information for further analysis. If you look at the graph I attached, you can see several "plateaus." I am trying to manually select 5 data points from each (equally spaced by 1, i.e. 30:1:35) and average the 5 points together. I am trying to repeat that method for 5 different "plateaus" and 3 different lines (roll, pitch, yaw). The code I have provided works, but I am worried when I duplicate the code for a second sensor's data, the variables (a-o) will be repeated and the code will not work properly. I have thought of making the second sensor's variables capitalized (i.e A-O), but I was wondering if there was a way to tidy up the bottom half of my code, starting at "%mean value for each plateau" (maybe by using for loops, if-then statements, etc). Any help will be appreciated!
%orientation data for sensor a
filename = 'exampleLogfile-000.txt';
fileIDa = fopen(filename);
Aa = textscan(fileIDa, '%f %f %f %f ', 'Headerlines', 5);
Rolla = Aa{1,2};
Pitcha = Aa{1,3};
Yawa = Aa{1,4};
plot(Rolla);
hold on
plot(Pitcha);
hold on
plot(Yawa);
%Manually select ranges for 5 plateaus
v = 30:35;
w = 160:165;
x = 320:325;
y = 500:505;
z = 650:655;
%Mean value for each plateau
a = mean(Rolla(v)); f = mean(Pitcha(v)); k = mean(Yawa(v));
b = mean(Rolla(w)); g = mean(Pitcha(w)); l = mean(Yawa(w));
c = mean(Rolla(x)); h = mean(Pitcha(x)); m = mean(Yawa(x));
d = mean(Rolla(y)); i = mean(Pitcha(y)); n = mean(Yawa(y));
e = mean(Rolla(z)); j = mean(Pitcha(z)); o = mean(Yawa(z));
% sensor 1 data for 5 different points
data1 (1:5, 1:3) = [a f k;b f l;c h m; d i n; e j o];
1 Comment
Stephen23
on 8 Dec 2017
@Takhyung Seon: if you are creating a whole long sequence of variable names like a, b, c, etc, then your approach is wrong. You should simply create one array (which could be a cell array).
Put the range index limits into two vectors, loop over the vectors, and extract those parts of Rolla into a cell array or ND array: simpler, expandable, and much more generalized.
Accepted Answer
More Answers (0)
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!