vertcat error due to Dimensions of arrays being concatenated are not consistent
1 view (last 30 days)
Show older comments
Ahmed Abdulhameed M Almahdi
on 29 May 2019
Commented: Ahmed Abdulhameed M Almahdi
on 13 Jun 2019
Hello,
I am trying to split text in excel file to be in multiple columns. the only problem I am facing with my code is that the dimensions of my arrays are not consistance ( vertcat error ), Is there another function I can use instead of vertcat to avoid such an error ?
thank you
this is how data looks like now
![Screen Shot 2019-05-28 at 8.55.52 PM.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/221621/Screen%20Shot%202019-05-28%20at%208.55.52%20PM.png)
My goal to have the data look like this
![Screen Shot 2019-05-28 at 8.55.35 PM.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/221622/Screen%20Shot%202019-05-28%20at%208.55.35%20PM.png)
My excel file is attached
My code:
clear
clc
x=readcell('DataOutcome.xlsx');
c=x(cellfun(@ischar,x));
split1=regexp(c, '\t', 'split');
bounds=strcat(vertcat(split1{:}))
0 Comments
Accepted Answer
Pullak Barik
on 10 Jun 2019
If you open 'split1' variable from the workspace and inspect its elements, it is easy to see why you are facing the issue.
What your code does is this- the rows containing the data outcome itself are read as cell arrays of length 31, where as the row containing the data for speed intervals is read as a cell array of length 30. This is because, for each row of data outcome, the first column is filled by 'Item i ' (where i is a particular row from the data outcome sub-table). Also, the headings 'Data Outcome' and 'Speed Interval' are being read as separate strings. Thus, it leads to unequal number of columns in each row, causing the mentioned error while using vertcat.
Here's a code that takes the above into consideration and manually adjusts the data-
clear;
clc;
x = readcell('DataOutcome.xlsx');
c = x(cellfun(@ischar, x));
split1=regexp(c, '\t', 'split');
bounds = vertcat(horzcat(split1{1, 1}, split1{2, :}), split1{3:10, :}, horzcat(split1{11, 1}, split1{12, :}));
The structure of the modified 'bounds' cell matrix is a little different from your goal screenshot, but only aesthetically.
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!