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
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
DavidL88 le 12 Jan 2021
Hi Adam
Thank you for your reply. I had thought the same but it's not clear to me how there could be a different number of columns. When I run the script, A1-4 all have the same number of columns. When I remove T from within the [] ie T1 = [A1] then the table T comes out fine so there's no problem combining it then (I seem to need the T within [] for looping as this was required in other scripts I ran).
Adam Danz
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()).
T = [A1;A2;A3;A4];
I suspect you intended.
DavidL88
DavidL88 le 12 Jan 2021
Hi Adam
I have the T variables as T1 = []; at the start of the script. It says the line causing the error is T1 = [T1; A1]'; I ran size as you suggested. It comes out different as below which would suggest this is the issue (it would be simpler to just run without T variables but it doesn't seem to work then when I loop it for a group of test files).
size(T1,2) = 1
size(A1,2) = 94
I ran T = [A1;A2;A3;A4]; and got the below message.
Error using vertcat
Dimensions of arrays being concatenated are not consistent. Consider converting input arrays to the same type before
concatenating.
Error in working_file (line 33)
T = [A1;A2;A3;A4];
All the A's for this one test are 1x94 double so 1 row and 94 columns. All the T's are blank.
For clarity, I copy the entire script below. Removing the T variables and just using T = table(A4, A1, A2, A3) after 'end' gives what I'm looking for when sFiles = one test. But when sFiles = all tests I get back a blank for all the A variables (A = []) and for the table (T = 0×4 empty table).
% Process: Select files using search query
sFiles = subject/test
T1 = [];
T2 = [];
T3 = [];
T4 = [];
for iFile = 1:length(sFiles)
DataMat = in_bst_data(sFiles(iFile).FileName);
tvalue = DataMat.tmap
pvalue = DataMat.pmap
time = DataMat.Time
col = (find(DataMat.pmap(1,:)<0.05));
[X Y]=size(col);
A1 = (tvalue(:,col));
A2 = (pvalue(:,col));
A3 = (time(:,col));
A4 = (repmat({DataMat.Comment},1,Y'));
T1 = [T1;A1]';
T2 = [T2;A2]';
T3 = [T3;A3]';
T4 = [T4;A4]';
end
T = table(T4, T1, T2, T3)
> 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)
ans = 1×2
0 0
Adam Danz
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
DavidL88 le 12 Jan 2021
I solved it for one test by deleting all T = [] and using T1-4 = (cell(X,Y)'); within the loop so the T variables are the same size as the A variables.
I placed this after the line with [X Y]=size(col);
T1 = (cell(X,Y)');
T2 = (cell(X,Y)');
T3 = (cell(X,Y)');
T4 = (cell(X,Y)');
When I set it sFiles to equal all test files the table comes out empty. The script is set so that only p values = 0.05 and their corresponding t values and latencies are returned. The last file in the loop has no p values <0.05. I'm not sure why the other values are not captured in the table T though. I can't upload the mat file because it's private data (from research). Thank you for your help.
Adam Danz
Adam Danz le 13 Jan 2021
Modifié(e) : Adam Danz le 13 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
DavidL88 le 13 Jan 2021
Hi Adam,
I solved it. Thanks for your help and the tip about debugging (I had not used it before). You are right that I wanted to concatenate all the A arrays side by side into one table. I had two issues which were linked but I wasn't sure how to fix. Concatenate the A variables with T variables and do this in a for loop. All the A arrays were the same size but usually >1 so couldn't seem to concatentate them with T variables which were set to = 1. I tried to set the T variables to same size as As within each for loop (or to remove the T variables and simplify the script) but the goal I was seeking, a table with the data from each iteration would be blank (it was bing written over with each iteration). I simply replaced the T1-4 = []; with T1-4 = cell(0,0);, placed before the for loop, and this seems to have fixed the issue.
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))]
ans = 4x3 table
Var1 Var2 Var3 _______ ________ _______ 0.76622 0.054515 0.67136 0.9878 0.71826 0.13674 0.93978 0.66781 0.75822 0.64564 0.35228 0.40694
By the way, you could have set T to T=[] or T=table(), too.
[table(); array2table(rand(3,3))]
ans = 3x3 table
Var1 Var2 Var3 _______ _______ _______ 0.17549 0.64745 0.8014 0.18883 0.42064 0.92881 0.53757 0.93268 0.49124
But I still don't know why T=[] didn't work for you,
[[]; array2table(rand(3,3))]
ans = 3x3 table
Var1 Var2 Var3 ________ _______ _______ 0.56871 0.88387 0.58633 0.080156 0.79495 0.56984 0.19062 0.60072 0.69567
DavidL88
DavidL88 le 13 Jan 2021
Thank you!
I wanted to accept your answer but can't see the option next to your comment?
Adam Danz
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!

Connectez-vous pour commenter.

 Réponse acceptée

Adam Danz
Adam Danz le 13 Jan 2021

0 votes

Summary of comments under the question,
Instead of initializing T as an empty cell, use an empty table.
T = table();
A1 = array2table(rand(3,3));
C = [T,A1]
C = 3x3 table
Var1 Var2 Var3 _______ _______ _______ 0.73391 0.42024 0.11362 0.81381 0.81976 0.94401 0.23762 0.68696 0.64335

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by