フィルターのクリア

graph multiple excel or csv files

2 ビュー (過去 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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by