Plot serial time data from excel file

조회 수: 16 (최근 30일)
Angel Lozada
Angel Lozada 2025년 10월 7일
답변: Star Strider 2025년 10월 8일
Hello.
I am trying to plot data from excel file. Column 2 (amplitude) vs Column 1 (time). The time is in second
Each second the data is sample 200 time.
0.000 (second zero sample zero) => 0.199 (second zero to sample 199) to second 120.004 (120 second. sample 004).
I try writing a code, but I am not able to make it work.
Also, I am trying to plot the results where xlabel show that each second there are 200 samples.
I have try some codes.
% Define the sampling parameters
Fs = 200; % Sampling frequency in Hz
duration = 5.199; % Duration in seconds
t = 0:1/Fs:duration; % Time vector from 0 to 5.199 seconds
% Read data from Excel file
data = readtable('HUAM1709.041_v3.xlsx'); % Replace with your actual file name
amplitude = data.Var2; % Assuming the second column contains the amplitude data
% Ensure the time vector matches the length of the amplitude data
if length(amplitude) ~= length(t)
error('Length of amplitude data does not match the time vector.');
end
% Plot the discrete time series data
figure;
stem(t, amplitude, 'filled'); % Use stem for discrete data representation
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Discrete Time Series Data from 0 to 5.199 seconds');
grid on;
This code was thanks to Star Strider (Level 10 Mathworks)
data = readtable('HUAM1709.041_v2.xlsx')
% return
T = data{:,1};
T = seconds(data{:,1});
T.Format = 'hh:mm';
T2 = data{:,2};
figure; % Place "figure" in this section allow to plot this data in a separate window.
plot (T,T2,'.');
title 'Accelerometer';
ylabel ('Amplitude');
xlabel ('Samples');
I am looking a plot like the attached file.
Where X axis is going from 0 to 120.

채택된 답변

Star Strider
Star Strider 2025년 10월 8일
Your data does not look like the data in the image file.
I made some changes (additions) to your code to plot only the 't' and 'amplitude' values you define in your code that correspond to the 'duration' limit. I used 'data.Var1' as the time vector. I then determined the length (in seconds) of 't' and calculated the logical vector 'Lv' to select only those values of 't' that are less than 'duration'. I then used that logical vector to extract a matching length section of 'amplitude'. (I linked to the Excel file you posted in your earlier Question.)
file = websave('HUAM1709.041_v2.xlsx','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1841254/HUAM1709.041_v2.xlsx')
file = '/users/mss.system.wzvhl/HUAM1709.041_v2.xlsx'
% Define the sampling parameters
Fs = 200; % Sampling frequency in Hz
duration = 5.199; % Duration in seconds
t = 0:1/Fs:duration; % Time vector from 0 to 5.199 seconds
% Read data from Excel file
% data = readtable('HUAM1709.041_v3.xlsx'); % Replace with your actual file name
data = readtable(file)
data = 24205×4 table
Var1 Var2 Var3 Var4 _____ _______ _______ _______ 0 -0.0157 0.0107 0.0111 0.001 -0.0758 0.0469 0.0666 0.002 0.0612 -0.0408 -0.0544 0.003 0.0336 -0.0217 -0.0299 0.004 -0.0843 0.0514 0.0757 0.005 0.0203 -0.0118 -0.0166 0.006 0.0733 -0.046 -0.0622 0.007 -0.0646 0.04 0.0563 0.008 -0.0326 0.0207 0.0275 0.009 0.0863 -0.0513 -0.0724 0.01 -0.0209 0.017 0.0189 0.011 -0.0723 0.0441 0.0642 0.012 0.0668 -0.0397 -0.0556 0.013 0.0312 -0.0193 -0.0294 0.014 -0.0839 0.0517 0.0748 0.015 0.0207 -0.0104 -0.017
amplitude = data.Var2; % Assuming the second column contains the amplitude data
% Fs_from_time_vector = 1/mean(diff(data{:,1}))
[tmin,tmax] = bounds(data{:,1})
tmin = 0
tmax = 121.0040
Lv = data.Var1 <= duration; % Logical Vector To Select Values Of The Time Vector ('data.Var1)' <= 'duration'
t_lim = data.Var1(Lv); % Limited Time Vector
amplitude_lim = amplitude(Lv); % Limited 'amplitude' Vector
% Ensure the time vector matches the length of the amplitude data
% if length(amplitude) ~= length(t)
% error('Length of amplitude data does not match the time vector.');
% end
% Plot the discrete time series data
figure;
% stem(t, amplitude, '.', 'filled'); % Use stem for discrete data representation
stem(t_lim, amplitude_lim , '.', 'filled')
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Discrete Time Series Data from 0 to 5.199 seconds');
grid on;
This uses your selected values to plot the selected variables.
Plotting the other ones similarly --
for k = 1:2
figure
plot(t_lim, data{Lv,k+2})
grid
xlabel('t')
ylabel('Variable')
title("Column "+(k+2))
end
..
This is the best I can do with your data.
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by