graph multiple excel or csv files

조회 수: 7 (최근 30일)
Willian
Willian 2024년 5월 15일
댓글: Voss 2024년 5월 17일
I have 4 excel files, which I would like to join using matlab into a single file like example 5 (but not convert it to excel). But using the time table command in which the date and time are combined, the purpose is to be able to graph various amounts of data quickly without graphing each file.
The purpose is when you have to graph excel or csv files to be able to generate them quickly
For example:
I really appreciate the help.
My code actual:
clear;
clc;
%% Load and plot data example 01
filename = 'example1';
T = readtable(filename);
x1 = T{:,1} + days(T{:,2}); % Combining date and time
y1 = T{:,3}; % Pressure data
figure
plot(x1, y1, 'r-', 'LineWidth', 1)
hold on
%% Load and plot data example 02
filename = 'example2';
T = readtable(filename);
x2 = T{:,1} + days(T{:,2}); % Combining date and time
y2= T{:,3}; % Pressure data
plot(x2, y2, 'b-', 'LineWidth', 1)
hold on
%% Load and plot data example 03
filename = 'example3';
T = readtable(filename);
x3 = T{:,1} + days(T{:,2}); % Combining date and time
y3= T{:,3}; % Pressure data
plot(x3, y3, 'g-', 'LineWidth', 1)
hold on
%% Load and plot data example 04
filename = 'example4';
T = readtable(filename);
x4 = T{:,1} + days(T{:,2}); % Combining date and time
y4= T{:,3}; % Pressure data
plot(x4, y4, 'k-', 'LineWidth', 1)
hold on
grid on
axis tight

채택된 답변

Voss
Voss 2024년 5월 15일
편집: Voss 2024년 5월 15일
% get info about the (4) files
S = dir('*.xlsx');
% construct full path file names
filenames = fullfile({S.folder},{S.name});
% read each file into a table, stored in the S struct array as 'data' field
for ii = 1:numel(S)
S(ii).data = readtable(filenames{ii});
end
% combine all tables into one
T = vertcat(S.data);
% remove rows with duplicate times
[t,idx] = unique(T.(1)+days(T.(2)));
T = T(idx,:);
% plot
figure
plot(t,T.(3))
grid on
axis tight
  댓글 수: 2
Willian
Willian 2024년 5월 17일
thank you very much for the support, very useful
Voss
Voss 2024년 5월 17일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2024년 5월 15일
편집: Mathieu NOE 2024년 5월 15일
hello
try this
if you have lot of excel files and you want to be sure they are sorted correctly (what the regular dir does not) please consider using this excellent submission :
code :
fileDir = pwd; % current directory (or specify which one is the working directory)
S = dir(fullfile(fileDir,'example*.xlsx')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see : https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
for k = 1:length(S)
filename = S(k).name % to actually show filenames are sorted (see command window)
T = readtable(fullfile(fileDir, filename));
x = T{:,1} + days(T{:,2}); % Combining date and time
y = T{:,3}; % Pressure data
plot(x,y);
hold on
end
  댓글 수: 1
Willian
Willian 2024년 5월 17일
Thank you very much for the information, a way to avoid disorder when graphing

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by