- Create a cell array or a table to store the merged data from all the CSV sheets.
- Use the dir function to obtain a list of all the CSV files in the directory.
- Iterate over the list of CSV files.
- Use the readmatrix or readtable function to read each CSV file into a temporary variable.
- Append the data from the temporary variable to the cell array or table created in step 1.
- After iterating over all the CSV files, write the merged data to a new CSV file using the writetable function.
is it possible to merge .csv sheet?
14 views (last 30 days)
Show older comments
i have around 30 .csv sheets like this
but here i have attached around 3 .csv sheets and final excel sheet has to be saved and how it should look like which is named as "final table.csv"
all values in trial1,trial2,trial3 has to be arranged in such way as shown in final table.csv
under component column if trial1 data is placed then "trial1" should be written in that column similarily others
i hv to arrange in descending order also
0 Comments
Answers (1)
Manan Jain
on 12 Jul 2023
Hi!
Yes it is possible to merge the CSV files into one. You can try the following steps:
directory = 'path/to/csv/files';
% Get list of CSV files
files = dir(fullfile(directory, '*.csv'));
% Create a cell array to store the merged data
mergedData = {};
for i = 1:length(files)
filePath = fullfile(directory, files(i).name); % reading the csv file
data = readmatrix(filePath);
% Append the data to the merged data array or table
mergedData = [mergedData; data];
end
% Write the merged data to new CSV file
outputFile = 'finaltable.csv';
writematrix(mergedData, outputFile, 'Delimiter', ',');
I hope this helps!
3 Comments
Manan Jain
on 12 Jul 2023
To Sort the data tables in decreasing order based on a specific column. For example, if you have a column named "Value" that determines the order, you can use the sortrows function:
sortColumn = 'Value';
for i = 1:numFiles
dataTables{i} = sortrows(dataTables{i}, sortColumn, 'descend');
end
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!