Clear Filters
Clear Filters

is it possible to merge .csv sheet?

11 views (last 30 days)
prabhu singh
prabhu singh on 12 Jul 2023
Commented: Manan Jain on 12 Jul 2023
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

Answers (1)

Manan Jain
Manan Jain on 12 Jul 2023
Hi!
Yes it is possible to merge the CSV files into one. You can try the following steps:
  1. Create a cell array or a table to store the merged data from all the CSV sheets.
  2. Use the dir function to obtain a list of all the CSV files in the directory.
  3. Iterate over the list of CSV files.
  4. Use the readmatrix or readtable function to read each CSV file into a temporary variable.
  5. Append the data from the temporary variable to the cell array or table created in step 1.
  6. After iterating over all the CSV files, write the merged data to a new CSV file using the writetable function.
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
prabhu singh
prabhu singh on 12 Jul 2023
i hv to arrange in descending order also can i get images to see
Manan Jain
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

Sign in to comment.

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!