# How to reduce angle values in a range between 0 and 360 deg?

24 views (last 30 days)
Loren99 on 29 Oct 2021
Commented: Star Strider on 30 Oct 2021
Hi everyone! I have obtained the angles alpha, beta and gamma in deg. However I would like to make a plot with values of gamma in a range between 0 and 360 deg (a sort of step-plot). Can anyone help me?
time = B(:,1);
num_times = size(time,1);
%assegno valori iniziali degli angoli di Eulero
vAlphaBetaGamma0 = [alpha0, beta0, gamma0];
[vTime, vAlphaBetaGamma] = ode45(@funzione,time,vAlphaBetaGamma0);
alpha = vAlphaBetaGamma(:,1);
beta = vAlphaBetaGamma(:,2);
gamma = vAlphaBetaGamma(:,3);
figure
plot(vTime,alpha,'k',vTime,beta,'r', vTime,gamma,'b');
legend('alpha','beta','gamma');
xlabel('Time (seconds');
ylabel('Angles (deg)');
function angularvelocities=funzione(t,attitudeangles)
end

Star Strider on 30 Oct 2021
I am not certain what the desired result is.
Something like this came up once before, and since wrap2pi and wrap2360 are available here (I do not have the Mapping Toolbox), I wrote a function to emulate both, comparing it to those functions while experimenting with the results here. (Replace ‘360’ with ‘2*pi’ and ‘180’ wiith ‘pi’ to get the wrap2pi equivalent, or other values to get different behaviour.)
See if this produces the desired result —
a = linspace(-720, 720);
figure
plot(a, wrap_180(a))
grid
figure
plot(a, wrap_360(a))
grid
function wP1 = wrap_180(P1) % EMulates 'wrap2180'
mf = mod(P1,360);
wP1 = mf.*(mf<=180) + (mf-360).*(mf>180);
end
function wP1 = wrap_360(P1) % EMulates 'wrap2360'
mf = mod(P1,720);
wP1 = mf.*(mf<=360) + (mf-720).*(mf>360);
end
.
##### 2 CommentsShowHide 1 older comment
Star Strider on 30 Oct 2021
My calculations do not agree with yours.
I get 19 revolutions, not 38
av = 0.4; % Angular Velocity (rad/s)
tvl = 600; % Record Length (s)
rrevs = av*tvl % Revolutions (rad)
rrevs = 240
drevs = string(rad2deg(rrevs)) % Revolutions (°) ('string' Call To Show PRecision)
drevs = "13750.9871"
tv = linspace(0, tvl, tvl); % Time Vector (s)
tot_revs = rad2deg(av) * tv; % Total Revolutions In 'tvl'
% endrevs = tot_revs(end)
Nr_revs = nnz(islocalmax(wrap_360(tot_revs))) % Total Number Of Revolutions (Counts Peaks)
Nr_revs = 19
figure
subplot(2,1,1)
plot(tv, tot_revs)
grid
xlabel('Time (s)')
ylabel('Degrees')
subplot(2,1,2)
plot(tv, wrap_360(tot_revs))
grid
xlabel('Time (s)')
ylabel('Degrees')
function wP1 = wrap_360(P1) % Emulates 'wrapTo360'
mf = mod(P1,720);
wP1 = mf.*(mf<=360) + (mf-720).*(mf>360);
end
I cannot test my code with the actual data without the actual data.
.

Walter Roberson on 29 Oct 2021
mask = 0 <= gamma & gamma <= 360;
Loren99 on 30 Oct 2021
@Walter Roberson I applied your suggestion, but that's not what I want to achieve. I would like to get a "zig zag plot", that is, when I get an angle value of 360 degrees (after one revolution), the plot goes back to zero and then starts growing again. 14000 degrees in the plot equals 23 revolutions per second.

R2021b

### Community Treasure Hunt

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

Start Hunting!