Create table from data
Afficher commentaires plus anciens
Hi
I'm trying to extract some data (A1-4) and place it into a table. I get the error message "Error using vertcat - Dimensions of arrays being concatenated are not consistent." Can you advise what this is and how I can fix it?
The overall script the below code is from is more complex. The purpose of it is to extract data from a large number of statistical tests from multiple subjects and place the data in one table. When I run the script on one file (one subject and one test) I get the same error message. When I remove T1-4 from within the [ ] below and run it on one file then the table comes out fine. However, if I do that with all my files in a loop then the table comes out empty. For the one file I tested it on; A1-3 are 1x94 double and A4 is 1x94 cell.
T1 = [T1; A1]';
T2 = [T2; A2]';
T3 = [T3; A3]';
T4 = [T4; A4]';
T = table(T4, T1, T2, T3)
13 commentaires
Adam Danz
le 12 Jan 2021
One of the lines is trying to combine data with a different number of columns. If this is expected, you can pad the matrix with fewer columns (padarray). If this is unexpected, perhaps the problem occurs when reading in the data.
DavidL88
le 12 Jan 2021
Adam Danz
le 12 Jan 2021
> ...it's not clear to me how there could be a different number of columns
How/why there are a different number of columns is a different question from wether there is a different number of columns or not. You should test whatever line is causing the error by directly looking at the number of columns using,
% (example for T1)
size(T1,2)
size(A1,2)
> When I run the script, A1-4 all have the same number of columns.
Do they have the same number of columns as T1? Is T1 empty at the start of the series of concatenations? BTW, why not simplify the vertical concatenation using,
T = [T1;A1:A2;A3;A4];
% or, if T1 is initially empty,
T = [A1:A2;A3;A4];
> When I remove T from within the [] ie T1 = [A1] then the table T comes out fine
That suggests that T1 does not have the same number of columns as A1. It would be helpful to know what line is causing the error.
To help further, I need to know the size of all T1, A1..A4 and, if they are tables, it might be helpful to see their first few rows (using head()).
Walter Roberson
le 12 Jan 2021
T = [A1;A2;A3;A4];
I suspect you intended.
DavidL88
le 12 Jan 2021
> I have the T variables as T1 = []; at the start of the script.... size(T1,2) = 1
There you go. T1 doesn't equal [] when you are concatenating it.
T = [];
size(T)
Adam Danz
le 12 Jan 2021
This would be a lot easier if you attach a mat file with the variables you're concatenating.
DavidL88
le 12 Jan 2021
It's getting more difficult to follow what's-what. It also seem like you're making the mistake of trying anything to fix the problem rather than understanding the problem.
From what I understand you have 4 arrays, A1 A2 A3 and A4 and you'd like to vertically concatenate those arrays which requires them to have the same number of columns but you're getting an error when you try to do this.
If the A# arrays should have the same number of columns but they do not, the solution is figuring why they don't have the same number of columns.
If the A# arrays might not have the same number of columns and you want to vertically concatenate them, you need to either trim the extra columns or pad the narrower arrays to match the max number of columns. Are you using Matlab's debug mode? If not, this would be a good time to learn by setting a break point just before the concatenation and looking at the values of the arrays (see demo).

DavidL88
le 13 Jan 2021
High-five!
I didn't know a cell could be concatenated to a table.
% cell-table concatentation
[num2cell(rand(1,3)); array2table(rand(3,3))]
By the way, you could have set T to T=[] or T=table(), too.
[table(); array2table(rand(3,3))]
But I still don't know why T=[] didn't work for you,
[[]; array2table(rand(3,3))]
DavidL88
le 13 Jan 2021
Adam Danz
le 13 Jan 2021
I copied the comment I think you're referencing to the answers section. Thanks for keeping the forum tidy!
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!