Clear Filters
Clear Filters

graph multiple excel or csv files

22 views (last 30 days)
Willian
Willian on 15 May 2024
Commented: Voss on 17 May 2024 at 22:36
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

Accepted Answer

Voss
Voss on 15 May 2024
Edited: Voss on 15 May 2024
% 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 Comments
Willian
Willian on 17 May 2024 at 15:46
thank you very much for the support, very useful
Voss
Voss on 17 May 2024 at 22:36
You're welcome!

Sign in to comment.

More Answers (1)

Mathieu NOE
Mathieu NOE on 15 May 2024
Edited: Mathieu NOE on 15 May 2024
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 Comment
Willian
Willian on 17 May 2024 at 15:47
Thank you very much for the information, a way to avoid disorder when graphing

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!