merge multiple csv file to one and draw a boxplot

5 views (last 30 days)
Hi ,
I have multiple csv files namely(50hz.csv, 10hz.csv,no_mot.csv) in E/data directory
Each file has 2 columns of data
I want to take 1st and 2nd column from all files and save as single csv.
and draw a box plot like this below pic
in place of
F1 50 hz
GOOD 1st column of 50hz.csv
BAD 2nd column of 50hz.csv
F2 10 hz
GOOD 1st column of 10hz.csv
BAD 2nd column of 10hz.csv
F2 10 hz
GOOD 1st column of no_mot.csv
BAD 2nd column of no_mot.csv
Could anyone from the community please hep me resolve this the problem?
Also I have attched sample data csv file here.
  2 Comments
Khushboo
Khushboo on 4 Nov 2022
Hello,
Do you want to concatenate all the csv files such that final one has 2 columns with data concatenated column wise?
Venkatkumar M
Venkatkumar M on 4 Nov 2022
Edited: Venkatkumar M on 4 Nov 2022
@Khushboo No I want to combine all csv file into one which has 6 columns.

Sign in to comment.

Answers (1)

Eshan Patel
Eshan Patel on 4 Nov 2022
Hey Venkatkumar,
I understand that your two objectives are to merge your CSV files into a single file and generate some boxplots from your original data.
To merge your files, you can use the following snippet of code:
load 50hz.csv;
load 10hz.csv;
load no_mot.csv;
mergedData = [X50; X10; no_mot];
save mergedData.csv;
This will save the required data into a file called "mergedData.csv" in your working directory.
To plot the data as you have described, you can use the following snippet of code to plot all your data:
% unwrap data into a single column vector
plotData = [X50(:,1); X50(:,2); X10(:,1); X10(:,2); no_mot(:,1); no_mot(:,2)];
% make groupings for the boxplot
g1 = repmat({'F1-Good'}, 90, 1);
g2 = repmat({'F1-Bad'}, 90, 1);
g3 = repmat({'F2-Good'}, 90, 1);
g4 = repmat({'F2-Bad'}, 90, 1);
g5 = repmat({'F3-Good'}, 90, 1);
g6 = repmat({'F3-Bad'}, 90, 1);
g = [g1; g2; g3; g4];
% plot your data
subplot(1,3,1);
boxplot(plotData(1:180), g(1:180), 'Labels', {'Good', 'Bad'});
xlabel('F1');
subplot(1,3,2);
boxplot(plotData(181:360), g(181:360), 'Labels', {'Good', 'Bad'});
xlabel('F2');
subplot(1,3,3);
boxplot(plotData(361:end), g(361:end), 'Labels', {'Good', 'Bad'});
xlabel('F3');
Alternatively, you can use a loop to plot the data after making the column vector and groupings:
n = 3; %number of files
left = 1;
right = 180; %number of data points in each file
for i = 1:n
% plot the data here
subplot(1, n, i);
boxplot(plotData(left:right), g(left:right), 'Labels', {'Good', 'Bad'});
xlabel("F" + i);
% update left and right variables to plot the correct data
range = right - left;
left = right + 1;
right = left + range;
end
You can refer to the documentation for boxplot for more information at the following link:
  1 Comment
Venkatkumar M
Venkatkumar M on 4 Nov 2022
Edited: Venkatkumar M on 4 Nov 2022
X50=load ('50.csv');
X10=load('10.csv');
no_mot=load ('no_mot.csv');
mergedData = [X50;no_mot];
save mergedData.csv;
plotData = [X50(:,1); X50(:,2);X10(:,1); X10(:,2);no_mot(:,1); no_mot(:,2)];
n = 3; %number of files
left = 1;
right = 180; %number of data points in each file
for i = 1:n
% plot the data here
subplot(1, n, i);
boxplot(plotData(left:right), g(left:right), 'Labels', {'Good', 'Bad'});
xlabel("F" + i);
% update left and right variables to plot the correct data
range = right - left;
left = right + 1;
right = left + range;
end
Error using load
Unable to find file or directory '50.csv'.
error message:
Index exceeds the number of array elements. Index must not exceed 360.
X50=load ('50.csv');
X10=load('10.csv');
no_mot=load ('no_mot.csv');
mergedData = [X50;no_mot];
save mergedData.csv;
plotData = [X50(:,1); X50(:,2);X10(:,1); X10(:,2);no_mot(:,1); no_mot(:,2)];
g1 = repmat({'F1-Good'}, 90, 1);
g2 = repmat({'F1-Bad'}, 90, 1);
g3 = repmat({'F2-Good'}, 90, 1);
g4 = repmat({'F2-Bad'}, 90, 1);
g5 = repmat({'F3-Good'}, 90, 1);
g6 = repmat({'F3-Bad'}, 90, 1);
g = [g1; g2; g3; g4];
subplot(1,3,1);
boxplot(plotData(1:180), g(1:180), 'Labels', {'Good', 'Bad'});
xlabel('F1');
subplot(1,3,2);
boxplot(plotData(181:360), g(181:360), 'Labels', {'Good', 'Bad'});
xlabel('F2');
subplot(1,3,3);
boxplot(plotData(361:end), g(361:end), 'Labels', {'Good', 'Bad'});
xlabel('F3');
Error Message
Error using boxplot>assignUserLabels
There must be the same number of labels as groups or as the number of elements in X.
Error in boxplot>identifyGroups (line 1261)
assignUserLabels(labels,groupIndexByPoint,numFlatGroups,xlen,...
Error in boxplot (line 291)
identifyGroups (gDat,grouporder,positions,colorgroup,...
And both code doesn't plot 3rd graph.
You are combinning the csv files veritically that has 2 cloumns as a final file but i want to combine it in a way final output has 6 columns of data from all 3 file.
Could you please help me to reoslve the issue?

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!