Error using cat Dimensions of arrays being concatenated are not consistent. Error in cell2mat m{n} = cat(1,c{:,n});

I'm trying to make an error bar plot with experimental data. i have used cell2table function to make a table some of the values on the table have [x,x] values like this with corresponding [y,y] and some of them are single values regardless i need to plot all of them against each other along with their error bars. but i keep getting this error on matlab.
% Scatter plot with error bars
figure;
errorbar(cell2mat(dataTable.FrictionAngle), cell2mat(dataTable.Cohesion), cell2mat(dataTable.Cohesion) - cell2mat(dataTable.CohesionMin), cell2mat(dataTable.CohesionMax) - cell2mat(dataTable.Cohesion), cell2mat(dataTable.FrictionAngle) - cell2mat(dataTable.FrictionAngleMin), cell2mat(dataTable.FrictionAngleMax) - cell2mat(dataTable.FrictionAngle), 'o');
please help

3 Comments

Can you save the dataTable variable to a MAT-file and attach it to your post? Edit your message (or add a comment) and use the paper clip button in the Insert section of the Toolstrip to attach the file.
Also please include the full and exact text of the error message (all the text displayed in the Command Window in red) in a comment. Don't paraphrase, don't summarize, don't truncate, copy and paste the full message.
My suspicion is that one or more of the variables in your table has a different size than you think they do and that's causing cell2mat (which shouldn't be necessary if your data is numeric) to return something differently sized.
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
Error in phiC (line 113)
errorbar(cell2mat(dataTable.FrictionAngle), cell2mat(dataTable.Cohesion), cell2mat(dataTable.Cohesion) - cell2mat(dataTable.CohesionMin), cell2mat(dataTable.CohesionMax) - cell2mat(dataTable.Cohesion), cell2mat(dataTable.FrictionAngle) - cell2mat(dataTable.FrictionAngleMin), cell2mat(dataTable.FrictionAngleMax) - cell2mat(dataTable.FrictionAngle), 'o');
^this is the whole message in the command window

Sign in to comment.

 Accepted Answer

load dataTable
NC = cellfun(@numel,dataTable.Cohesion);
Cmin = repelem(dataTable.CohesionMin,NC,1);
Cmax = repelem(dataTable.CohesionMax,NC,1);
NFA = cellfun(@numel,dataTable.FrictionAngle);
FAmin = repelem(dataTable.FrictionAngleMin,NFA,1);
FAmax = repelem(dataTable.FrictionAngleMax,NFA,1);
FA = [dataTable.FrictionAngle{:}].';
C = [dataTable.Cohesion{:}].';
% Scatter plot with error bars
figure
errorbar(FA,C,C-Cmin,Cmax-C,FA-FAmin,FAmax-FA,'o');

14 Comments

Hi @Voss i have to create the same plot for a different dataset but there is an error because it has a negative value so replemm cant be used is there anyway for me to fix this?
repelem works with negative data.
repelem([-1 2 -3 4], 2, 3)
ans = 2x12
-1 -1 -1 2 2 2 -3 -3 -3 4 4 4 -1 -1 -1 2 2 2 -3 -3 -3 4 4 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or are you saying that the replication factors are negative?
repelem([-1 2 -3 4], -2, 3)
Error using repelem
Replication factors must be nonnegative integer-valued scalars or vectors.
In the code Voss posted the replication factors are computed using numel or are the literal value 1. If numel returns a negative value, something's gone badly wrong. I'd make sure you haven't written your own numel function that's taking precedence over the built-in numel function in MATLAB. Check this using the which function.
which -all numel
Error using repelem
Replication factors must be nonnegative integer-valued scalars or
vectors.
Error in Specific (line 145)
CMmin = repelem(MarsRegolith.CohesionMin,NCM,1);
this is the error that i am getting, numel itself gives me just ones, i think it's the MarsRegolith.CohesionMin that gives the negative value.
What class and size is MarsRegolith?
whos MarsRegolith
If as I suspect MarsRegolith is a struct array that is not scalar (1-by-1), that code creates a comma-separated list (as per the second entry in the Generating a Comma-Separated List section on that document page.) That's being treated as the first N input arguments to repelem (N is the number of elements in the struct array) rather than being used as the first input argument.
Concatenate the elements of that comma-separated list into an array as shown in the Concatenation entry in the How to Use Comma-Separated Lists section on that documentation page) and pass the array that was created by that concatenation into repelem.
Please save MarsRegolith to a mat file and upload the mat file here.
The types and sizes of data stored in this table are quite different than the original table, so the code will be different.
Also, in this table, Cohesion is the same as CohesionMin is the same as CohesionMax, except that Cohesion is stored in a cell array. Same for FrctionAngle/Min/Max. Therefore the errorbars all have zero length.
load MarsRegolith
MarsRegolith
MarsRegolith = 8x7 table
Data FrictionAngle Cohesion CohesionMin CohesionMax FrictionAngleMin FrictionAngleMax _____________________________________ __________________________ _________________________ ______________ ______________ ________________ ________________ {'MPF Sojourner: Cloddy' } {[31.4000]} {[42.2000]} {[-0.3400]} {[0.5700]} -0.34 0.57 -0.34 0.57 31.4 42.2 31.4 42.2 {'MPF Sojourner: Drift' } {[15.1000]} {[33.1000]} {[ 0.1800]} {[0.5300]} 0.18 0.53 0.18 0.53 15.1 33.1 15.1 33.1 {'Viking Lander 1: Drift' } {[17.6000]} {[20.4000]} {[ 0.4000]} {[2.8000]} 0.4 2.8 0.4 2.8 17.6 20.4 17.6 20.4 {'Viking Lander 1: Blocky' } {[28.4000]} {[33.2000]} {[ 2.4000]} {[7.8000]} 2.4 7.8 2.4 7.8 28.4 33.2 28.4 33.2 {'Viking Lander 1 and 2: Rocks' } {[ 40]} {[ 60]} {[ 1000]} {[ 10000]} 1000 10000 1000 10000 40 60 40 60 {'Viking Lander 2: Crusty to Cloddy'} {[29.8000]} {[39.2000]} {[ 0.3000]} {[1.9000]} 0.3 1.9 0.3 1.9 29.8 39.2 29.8 39.2 {'MPF Sojourner Sand' } {[ 30]} {[ 30]} {[ 0]} {[ 1]} 0 1 0 1 30 30 30 30 {'Phoenix' } {[ 29]} {[ 47]} {[ 0.2000]} {[1.2000]} 0.2 1.2 0.2 1.2 29 47 29 47
C = cell2mat(MarsRegolith.Cohesion);
FA = cell2mat(MarsRegolith.FrictionAngle);
Cmin = MarsRegolith.CohesionMin;
Cmax = MarsRegolith.CohesionMax;
FAmin = MarsRegolith.FrictionAngleMin;
FAmax = MarsRegolith.FrictionAngleMax;
figure
errorbar(FA,C,C-Cmin,Cmax-C,FA-FAmin,FAmax-FA,'o');
oh i see is there away for me to make it so that the error bars correspond to each line ?
They do. They're all zero-length because the min and the max are the same.
Voss said, "The types and sizes of data stored in this table are quite different than the original table"
One big difference is that in dataTable, Cohesion and FrictionAngle are "ragged", with some rows having two values and others only one, while in MarsRegolith, those both have two values in every row. Using cell arrays is a necessary complication in the former, while it is just overcomplicating things in the latter.

Sign in to comment.

More Answers (1)

Hi Amanath,
The error is due to inconsistents in the sizes of elements wthin the cell arrays dataTable.Cohesion and dataTable.FrictionAngle .
For a successful conversion of a cell array into a MATLAB array, it's neccessary that all cells are of uniform size. However in the columns mentioned, there's a mix of single-element cells alongside cells containing two elements. This discrepancy in cell sizes is what led MATLAB to generate an error.
load("dataTable")
dataTable.Cohesion([1 2])
ans = 2x1 cell array
{[0.3500 0.7000]} {[ 0.4000]}
%Similarly for FrictionAngle
dataTable.FrictionAngle([1 2])
ans = 2x1 cell array
{[35 70]} {[ 40]}
Hope its clear

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!