how to plot a diagram that includes many excel data?

hello everyone. I have a problem with finding an approprieate program in matlab.
I have specifically 173 excel files including a sheet of time data and a sheet of many concentration data from different chemical compounds. The excel files are connected as the time continues in every file. My task is to find the averages of both time and concentration per 5 minutes and per 1 hour and plot seperately each chemical compound concentration with time ( one diagram every time). The problem is that i dont know how to insert all of the excel files and connect the data

5 Comments

I suggest to split the work in two steps
1/ make a loop to read all your excel files and generate a single array containing all your excel data
2/ then do the stats (averages )
for topic 1 , you can use this template code (adapt to your own needs)
in case you have all files in the same folder
fileDir = pwd; % current directory (or specify which one is the working directory)
outfile = 'OUT.xlsx'; % output file name
S = dir(fullfile(fileDir,'data00*.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)
max_rows = 1e4; % rough estimate (by excess) of max number of rows
out_data = [];
max_nn = 0;
for k = 1:length(S)
filename = S(k).name % to actually show filenames are sorted (see command window)
out = readmatrix( fullfile(fileDir, filename),"Range",'C:C'); % extract column 3 (C) of each xlsx files
nn = numel(out);
tmp = NaN(max_rows,1); % initiate tmp vector with NaN's
tmp(1:nn) = out; % fill tmp vector with data (from the top)
out_data = [out_data tmp]; % vertical concatenation
max_nn = max(max_nn,nn); % store longest file rows qty (see below : retrieve trailing nan's)
end
% retrieve trailing nan's
out_data = out_data(1:max_nn,:);
% store out_data in excel file
writematrix(out_data,fullfile(fileDir,outfile));
in case you have multiple folders , you can use that code (again, adapt to your own needs)
%% define path
yourpath = pwd; % or your specific path
list=dir(yourpath); %get info of files/folders in current directory
isfile=~[list.isdir]; %determine index of files vs folders
dirnames={list([list.isdir]).name}; % directories names (including . and ..)
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames))); % remove . and .. directories names from list
%% demo for excel files
sheet = 1; % specify which sheet to be processed (my demo) - if needed
%% Loop on each folder
for ci = 1:length(dirnames) %
fileDir = char(dirnames(ci)); % current directory name
S = dir(fullfile(fileDir,'data_*.xlsx')); % get list of data files in directory according to name structure 'Sheeta*.xlsx'
S = natsortfiles(S); % sort file names into natural order (what matlab does not) , see FEX :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
%% Loop inside folder
for k = 1:length(S) % read data in specified sheet
data = xlsread(fullfile(fileDir, S(k).name),sheet); % or use a structure (S(k).data ) to store the full data structure
% your own code here for data processing. this is just for my demo
% for now :
title_str = [fileDir ' / ' S(k).name ' / sheet : ' num2str(sheet)];
figure,plot(data),title(title_str);
end
end
"The problem is that i dont know how to insert all of the excel files and connect the data"
Use dir to get all the excel files.
Then, use readtable/readmatrix to read the data and subsequently perform operations accordingly.
See the 2nd example on this documentation page for example - https://in.mathworks.com/help/matlab/import_export/process-a-sequence-of-files.html
Are the times in each file sequential between files (so the end time of one file is the beginning of the next file) and all the columns (variables) are the same, or are the times all the same (or close to being the same times)?
If they are sequential and have the same variables, you can vertically concatenate the files easily, however if they are all the same times, that would require either outerjoin or synchronize to horizontally concatenate the tables correctly.
Please provide at least two of the files. Use the paperclip icon in the top toolstrip (to the right of the Σ symbol) to attach the files.
these are two of the excel files. all of them are in the same pattern
the sheets that i use are timecycle and concentration

Sign in to comment.

 Accepted Answer

One approach is to read the files in to separate cell arrays, then vertically concatenate them, and then choose the ‘Absolute Time’ column from the concatenated ‘Time’ sheet and horizontally concatenate it with the ‘Concentration’ sheet to get the result.
Try this —
files = dir('*.xlsx');
for k = 1:numel(files)
shnm = sheetnames(files(k).name);
% Choose_Sheets = shnm([2 4])
Time{k,:} = readtable(files(k).name, 'VariableNamingRule','preserve', 'Sheet',2);
Start_Stop = Time{k}([1 end],:)
Conc{k,:} = readtable(files(k).name, 'VariableNamingRule','preserve', 'Sheet',4);
end
Start_Stop = 2×3 table
Cycle number Absolute Time Relative Time ____________ ____________________ _____________ 1 02-Oct-2023 12:15:27 11.508 1251 02-Oct-2023 16:15:13 14398
Start_Stop = 2×3 table
Cycle number Absolute Time Relative Time ____________ ____________________ _____________ 1 02-Oct-2023 16:15:25 14410 1251 02-Oct-2023 20:15:10 28795
Timevc = vertcat(Time{:});
Concvc = vertcat(Conc{:});
Time_Conc = [Timevc(:,2) Concvc]
Time_Conc = 2502×49 table
Absolute Time m/z 41.00 ch1 m/z 31.00 ch3 m/z 33.00 ch5 m/z 42.00 ch7 m/z 43.00 ch8 m/z 45.00 ch9 m/z 47.00 ch10 m/z 57.00 ch11 m/z 59.00 ch12 m/z 61.00 ch13 m/z 63.00 ch14 m/z 66.00 ch15 m/z 69.00 ch16 m/z 71.00 ch17 m/z 73.00 ch18 m/z 75.00 ch19 m/z 79.00 ch20 m/z 81.00 ch21 m/z 83.00 ch22 m/z 85.00 ch23 m/z 87.00 ch24 m/z 91.00 ch25 m/z 93.00 ch26 m/z 95.00 ch27 m/z 99.00 ch28 m/z 101.00 ch29 m/z 103.00 ch30 m/z 105.00 ch31 m/z 107.00 ch32 m/z 113.00 ch33 m/z 115.00 ch34 m/z 121.00 ch35 m/z 129.00 ch36 m/z 135.00 ch37 m/z 137.00 ch38 m/z 139.00 ch39 m/z 147.00 ch40 m/z 149.00 ch41 m/z 151.00 ch42 m/z 163.00 ch43 m/z 181.00 ch44 m/z 88.00 ch45 m/z 59.00 ch46 m/z 169.00 ch47 m/z 155.00 ch48 m/z 211.00 ch49 m/z 225.00 ch50 m/z 247.00 ch51 ____________________ _____________ _____________ _____________ _____________ _____________ _____________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ _______________ ______________ ______________ _______________ _______________ _______________ _______________ _______________ 02-Oct-2023 12:15:27 3.6024 1.5815 14.218 0.35421 3.3947 3.5504 3.9886 8.6435 4.9049 2.8098 0.80691 0.15605 0.45607 0.15038 1.0485 0.14984 0.4506 0 0 0.075618 0.15167 1.5257 1.6068 0.30694 0.23136 0.37092 0.30957 0.077469 1.3949 0 0 0.43205 0.2306 0.21488 0.21485 0 0.082288 0.084869 0 0.12916 0 0 4.9894 0 0.096717 0 0 -748.93 02-Oct-2023 12:15:38 3.0883 2.2429 10.082 0.6698 3.6681 2.3695 3.2325 7.7053 4.5233 1.3395 0.52314 0 0.43121 0.12187 0.48555 0 0.24345 0.12196 0 0.061282 0.43019 0.61824 0.93013 0.18656 0.1875 0.2505 0.25088 0.31391 0.92949 0.3137 0.12539 0.45017 0.18688 0.17414 0.12437 0.12473 0.066687 0.20634 0.071367 0.10467 0 0 4.3176 0 0.078381 0 -606.94 0 02-Oct-2023 12:15:50 3.1293 1.2239 11.003 0.54825 0.87572 3.3942 5.865 5.3514 4.974 1.2791 0 0 0.058826 0.46552 0.5796 0.57979 0.058121 0.058234 0.23347 0.23409 0.17606 0.59039 0.94744 0.23754 0.3581 0.28705 0.23958 0 0.98358 0.23965 0 0.42989 0 0.11878 0.071261 0 0 0.065681 0 0.099957 0 0 5.2357 0 0.07485 0 0 0 02-Oct-2023 12:16:01 3.5889 1.3704 7.7003 0.51156 1.9611 4.5243 4.666 8.239 5.1296 2.8645 0.41953 0 0.1976 0.39093 0.64897 0.12984 0.065077 0.19561 0.26142 0.26211 0.39427 1.3221 1.5913 0.19948 0.20048 0.3482 0 0.20139 1.0476 0.20125 0.26815 0.40112 0.26643 0.1596 0.18618 0.26673 0 0.14708 0.07631 0.11192 0 0 6.6685 0.1607 0 0 0 0 02-Oct-2023 12:16:13 3.2472 2.7097 17.594 0.44956 0 3.0217 3.3411 7.8986 5.6671 1.888 0.12289 0 0.23154 0 0.45625 0.1141 0.11438 0.1146 0.11487 0 0.28874 1.7428 1.0488 0.11687 0.23491 0.28246 0 0.11799 0.75538 0.11791 0.058912 0.47001 0.1756 0.046752 0.046747 0.058601 0 0 0 0 0 0 6.1823 0 0.1473 0 0 0 02-Oct-2023 12:16:24 3.0522 2.264 10.177 0.25354 4.8598 4.3352 3.2833 6.8057 4.7215 1.6563 0.34655 0 0.21764 0.32292 0.7505 0.75076 0.16127 0.10772 0.10797 0.10825 0.21712 0.54606 1.0406 0.10985 0.38642 0.2655 0 0.16636 0.73222 0.11083 0.11075 0.37552 0.11004 0.10986 0.10985 0 0 0 0.12607 0 0 0 5.9927 0 0.20769 0 0 -536.07 02-Oct-2023 12:16:36 2.8783 1.9641 15.135 0.92171 1.6061 2.5196 2.8306 7.9744 5.6414 1.8768 0.11453 0 0.16183 0.42688 0.5315 0.10633 0.21319 0.053401 0.10705 0.053665 0.16145 0.54139 0.76021 0.054456 0.16419 0.21936 0 0.21991 0.87995 0 0.054902 0.63513 0.054551 0.065356 0.087129 0.054612 0 0 0.062496 0.091662 0 0 6.4216 0 0 0 0 0 02-Oct-2023 12:16:47 3.0769 1.4347 12.898 0.34276 7.3911 2.7283 3.4737 6.2731 4.419 2.0392 0.35137 0 0.331 0.32741 0.54353 0.21749 0.10901 0.16383 0.27368 0.21952 0.55036 2.2146 1.2217 0.11138 0.055971 0.33649 0 0.22489 0.7874 0 0.056145 0.42554 0.16736 0.13367 0.044552 0.055849 0 0.12319 0.063912 0 0 0 5.2168 0.13459 0 0 0 0 02-Oct-2023 12:16:59 2.7264 1.7892 12.064 0.71244 0.85348 4.5682 3.1589 8.4753 4.8477 2.4933 0.36517 0 0.172 0.22685 0.90381 0.22603 0.22658 0.056755 0.17066 0.11407 0.22879 1.1508 1.3274 0.057877 0.17451 0.32639 0 0.058432 0.8417 0.058392 0.05835 0.60519 0 0.16207 0.1389 0 0 0 0 0 0 0 5.9958 0.27975 0 0 0 0 02-Oct-2023 12:17:10 2.7664 1.7002 15.286 0.677 2.433 3.4428 4.1452 4.956 5.5762 2.0139 0.57833 0 0.21792 0.43112 0.53677 0.10739 0.10765 0.10786 0.21622 0.054198 0.27176 0 1.2613 0.10999 0.11055 0.288 0 0.11105 0.88869 0.16646 0.055447 0.53084 0.16528 0.11001 0.065996 0.055154 0 0 0.063117 0 0 0 5.6368 0 0 0 0 0 02-Oct-2023 12:17:22 2.9668 0.88912 14.655 0.44254 1.6965 3.6008 3.7375 9.7191 4.1839 2.7258 0.60487 0 0.11396 0.33818 0.56141 0.44928 0.33778 0.22562 0.28269 0.3968 0.22738 1.1437 1.0898 0.11504 0.28906 0.37073 0.23206 0.11615 0.90624 0.058033 0.11598 0.4858 0.17286 0.2071 0.11504 0.057686 0 0.12724 0 0 0 0 6.2125 0.13902 0.145 0 0 0 02-Oct-2023 12:17:33 3.0033 1.3923 13.769 0.91477 3.9849 2.6477 3.7926 3.6527 6.0754 1.979 0.341 0 0.10708 0.21183 0.211 0.21107 0.26448 0.21199 0.26561 0.15978 0.10682 1.0746 1.1856 0.10809 0.16295 0.17416 0 0.16369 1.0698 0.054526 0.16346 0.47819 0.10828 0.23783 0.064855 0.0542 0 0 0 0 0 0 5.8967 0.13062 0 0 0 0 02-Oct-2023 12:17:45 3.0914 2.3609 10.613 0.52879 2.5339 2.4942 2.9772 10.968 6.0598 1.4805 0.6023 0 0.11348 0.2245 0.44721 0.22368 0.16817 0.22466 0.22519 0.22578 0.39622 0.56943 1.1994 0.057276 0 0.32301 0 0.11565 0.90238 0.057786 0.23098 0.57587 0.17213 0.22913 0.11455 0 0 0 0 0 0 0 6.1861 0 0.072193 -559.02 0 0 02-Oct-2023 12:17:56 2.6209 1.9345 13.665 0.33012 1.5819 3.7956 4.6003 9.6669 6.0294 1.5019 0.22561 0 0.21253 0.10511 0.6282 0.3142 0.20998 0.31558 0.10544 0.26429 0.31804 0 1.0162 0.21455 0.053907 0.30248 0.64917 0.27075 0.8017 0.16234 0.10815 0.62557 0 0.042915 0.042909 0.10758 0 0.059323 0 0 -1047 0 5.3201 0 0.067605 0 0 0 02-Oct-2023 12:18:08 3.2821 2.8198 16.901 0.37427 2.6901 4.1376 3.0028 7.5346 4.6911 1.5718 0.25578 0 0.18071 0.11917 0.3561 0.23748 0.4166 0.11926 0.23907 0.17977 0.42066 1.2091 1.3946 0.18243 0.061115 0.24495 0 0.061391 0.93347 0.18405 0.061306 0.41574 0.12183 0.19461 0.17026 0.060982 0.065211 0.13451 0 0.10235 0 0 5.0932 0 0 0 0 0 02-Oct-2023 12:18:19 3.3766 1.7959 16.146 0.53631 0.85664 3.7946 3.4725 11.124 6.9143 1.5015 0.4887 0.11814 0.34527 0.68307 0.45358 0.3403 0.28427 0 0.17129 0.11449 0.34445 1.1551 1.1585 0.058091 0.17515 0.2106 0 0.1173 0.89175 0.11722 0.3514 0.44389 0.058192 0.16268 0.092946 0 0 0.19275 0 0.09778 0 0.22997 4.8656 0 0.07322 0 0 0
VN = Time_Conc.Properties.VariableNames;
figure
plot(Time_Conc{:,1}, Time_Conc{:,2:6}) % Plot Some Example Data
grid
xlabel('Time')
ylabel('Concentration')
title('Concentration Data')
legend(VN{2:6}, 'Location','best')
EDIT — (29 Feb 2024 at 01:37)
If the Excel files are not imported in sorted order, to sort them by time, add:
Time_Conc = sortrows(Time_Conc,1);
after creating the concatenated ‘Time_Conc’ table (in my code, name it appropriately for your needs).
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!