I am taking two excel files with similar x value data and want to plot the y values of one of the plots with different colors based on if the second file reads 0 or 1.

22 views (last 30 days)
I am importing two excel files that have a good amount of data in them. What I want out of the first is the g values from the accelerometer data over time, and what I want from the second is the 0 or 1 value from the On(T/F).
The time recorded for each file was recorded from different equipment so they do not align exactly but are close, but there is a lot more data in data2 than in data1
I want to plot the g-values from Data1 over the relative time but have the plot be red when On(T/F) reads 1 and a different color when it reads 0.
Below is the code I have so far to read the data.
clc, clear;
data1 = readtable('Data1.csv');
data2 = readtable('Data2.csv');
g_value = sqrt(data1.XAccel_g_.^2 + data1.YAccel_g_.^2 + data1.ZAccel_g_.^2);
time1 = data1.RelativeTime;
figure
plot(time1,g_value)
title('Data1')
xlabel("Time (s)")
ylabel("g")
time2 = data2.RelativeTime;
emiss = data2.On_T_F_;
emiss(emiss==0)=nan;
figure
stem(time2,emiss,'LineStyle','none','Marker','_')
legend("Laser On")
xlabel("Time (s)")
ylabel("T/F")

Answers (2)

Voss
Voss on 30 Oct 2024 at 18:46
warning off all
unzip('Data1.zip')
unzip('Data2.zip')
data1 = readtable('Data1.csv');
data2 = readtable('Data2.csv');
g_value = sqrt(data1.XAccel_g_.^2 + data1.YAccel_g_.^2 + data1.ZAccel_g_.^2);
time1 = data1.RelativeTime;
% interpolate data2's On_T_F_ to data1's RelativeTime using
% nearest-neighbor interpolation:
emiss = interp1(data2.RelativeTime,data2.On_T_F_,data1.RelativeTime,'nearest');
% gonna plot g_value twice, except the second one will have NaNs where
% emiss is 0 (so it won't show up in those regions)
g_value2 = g_value;
g_value2(emiss==0)=nan;
figure
subplot(2,1,1);
plot(time1,g_value)
hold on
plot(time1,g_value2)
title('Data1')
xlabel("Time (s)")
ylabel("g")
subplot(2,1,2);
plot(data1.RelativeTime,emiss)
ylim([-0.5 1.5])
% a zoomed-in copy of the same figure, to show some detail
xlim(copyobj(gcf,groot).Children,[1000 1400])

Star Strider
Star Strider on 30 Oct 2024 at 19:02
This is something of a challenge. In order to be able to use both records, it is necessary to intterpolatee the shorter record to the length of the longer record. That is straightforward, and allows them to be plotted together.
This is the best I can do with your data —
zips = dir('*.zip');
Uz1 = unzip(zips(1).name);
Uz2 = unzip(zips(2).name);
% data1 = readtable('Data1.csv');
% data2 = readtable('Data2.csv');
data1 = readtable(Uz1{:}, 'VariableNamingRule','preserve');
data2 = readtable(Uz2{:}, 'VariableNamingRule','preserve');
% data12 = synchronize(table2timetable(data2), table2timetable(data1), data2.('Absolute Time'))
g_value = sqrt(data1.('X Accel (g)').^2 + data1.('Y Accel (g)').^2 + data1.('Z Accel (g)').^2);
time1 = data1.('Relative Time');
time2 = data2.('Relative Time');
emiss = data2.('On (T/F)');
% [t11,t12] = bounds(time1)
% [t21,t22] = bounds(time2)
g_valuei = interp1(time1, g_value, time2, 'pchip','extrap'); % Interploate ‘g_value’ From ‘time1’ To ‘time2’
emiss(emiss == 0) = NaN; % This Eliminates ‘Bridging’
figure
plot(time2, g_valuei, '-b')
hold on
plot(time2, g_valuei.*emiss,'-r')
hold off
title('Data1')
xlabel("Time (s)")
ylabel("g")
figure
stem(time2,emiss,'LineStyle','none','Marker','_')
legend("Laser On")
xlabel("Time (s)")
ylabel("T/F")
.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!