9 views (last 30 days)

I have a cell array C (140x1). Each cell contains a 1200x2 double. What I want to ultimately achieve is one plot of multiple boxplots (one boxplot for the second column of each cell - C{i,1}(:,2)) without having to type out an individual line 140 times (because I'll have to do this for many different batches of files).

P = 'C:\filepath';

S = dir(fullfile(P,'*.csv'));

N = numel(S);

M = cell(N,1);

C = cell(N,1);

for k = 1:N

C{k} = csvread(fullfile(P,S(k).name));

M{k} = [M, C{k,1}(:,2)];

end

###Error message:

###Error using horzcat

###Dimensions of arrays being concatenated are not consistent. Consider converting

###input arrays to the same type before concatenating.

I've tried to separate out the cell array in the loop into 140 variable, I've tried to only read one column of the .csv file, and I've tried to create the boxplot inside the loop, but nothing I try seems to work.

My ultimate aim is something that looks like this:

But this was created painfully (manually), and I don't have the time to do this for every batch of files.

Does anyone have any ideas? Thanks

Constantino Carlos Reyes-Aldasoro
on 23 Oct 2019

I think that the problem is that you are using CELL, instead of a simple matrix. See my example:

C =[];

for k=1:120

C1 = k*rand(1200,1);

C2 = k*ones(1200,1);

C = [C;[C1 C2]];

end

boxplot(C(:,1),C(:,2))

I allocate a matrix that is empty, but will be adding in the first column a series of random numbers, just to make them different I multiply by k which is the counter. This counter is the one that you will use to iterate over your data. In the second column I just place the value of k, which will group all the values that you have in that group. Append over as you calculate.

Chage your variable A from a cell to a matrix and store your values in the first column, and the group in the second. That should work.

Constantino Carlos Reyes-Aldasoro
on 22 Oct 2019

Hi

What I would do would be to loop over your cell, read the values that you want to display (second column right?) and place in a new matrix (not cell) in the first column, and in the second column place the value of the current cell position, so that you create a matrix with 2 columns and 140X1200 rows, the first column would have the values and the second the group that will be used to create the boxplot.

If you have many categories, but some of these are related, you might consider displaying as 3D boxplots:

Hope this helps.

Constantino Carlos Reyes-Aldasoro
on 1 Nov 2019

Did the previous method help you resolve your question?

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.