Trying to concentrate a cell containing tables
Ältere Kommentare anzeigen
Hi guys
I have quite a big cell array here with each cell contains a 1x6 table.
I'm trying to take the data out of each cell out to present all of it on one table...
It doesn't let me use the vertcat though because the first column of each one of these tables is of the type "datetime" and I get this message:
% Error using concentrate (line 6)
% An error occurred when concatenating the table variable 'Date' using VERTCAT.
%
% Caused by:
% Error using datetime/vertcat (line 1348)
% All inputs must be datetimes or date/time character vectors or date/time strings.
By the way, I don't mind removing the whole date column... I tried that, it might be why I'm getting this error message...
I want to go from state 1 here to state 2:
THANKS TO ANYBODY WHO HELPS!


9 Kommentare
Image Analyst
am 1 Apr. 2020
How big? Millions of rows? Can you attach a small portion of it in a .mat file so people can try things?
You can identify which tables are not in datetime format by using the following line. The output, isNotDT is a logical vector the same size as your cell array c where true values mark tables that do not have datetime format in the chosen column.
% Which tables are not datetime?
isNotDT = cellfun(@(T)~isdatetime(T.Var1), c); % T.Var1 is your datetime column
% or
isNotDT = cellfun(@(T)~isdatetime(T(:,1), c); % Date time columns is column 1
To remove the 1st column from all tables,
% Remove 1st col
T_noDT = cellfun(@(T){T(:,2:end)}, c)
Or Shem Tov
am 1 Apr. 2020
Or Shem Tov
am 2 Apr. 2020
Adam Danz
am 2 Apr. 2020
Great!
cellfun(function, cellarray) applies a function to each element of a cell array.
The function is @(T){T(:, 2:end)} which is an anonymous function. The input is a table T. T(:, 2:end) extracts all of the columns of T except the first one. The result is stored in a cell. So, the output to cellfun() in this case is another cell array the same size as the input cell array and each element is a table that's missing the first column from the input tables.
Adam Danz
am 2 Apr. 2020
Or Shem Tov' answer moved here as a comment.
Thank you!
I also forgot to mention, seems like some of the rows have {0×0 double} in them, they turn out to be missing data, what if I wanna join all the rows without losing their index number?
seems when I'm using verdcat it shrinks the row number from 34k to 31k because of the missing data
Adam Danz
am 2 Apr. 2020
If I understand correctly, some of your cells are empty; they don't contain a table.
Based on your screen shots, I assume your tables all contain 1 row and 6 columns (reduced to 5 columns).
If you want to preserve the index number of each table after concatenating them, you'll need to fill the empty cell arrays with tables that contain missing values. That way, when you concatenate the 1-row tables, the rows of NaN values will maintain the index order.
Since this seems to be addressing your main question, I'll continue in the answers section.
Or Shem Tov
am 2 Apr. 2020
Adam Danz
am 2 Apr. 2020
Yes, I understand.
But, you cannot have a blank row within a table of numeric values. It's not possible.
You can, however, use NaN values to fill the empty rows which is what I'm suggesting you do.
Example:
7×4 table
Var1 Var2 Var3 Var4
____ ____ ____ ____
8 1 4 2
8 2 9 2
3 8 4 4
2 3 2 8
NaN NaN NaN NaN
4 1 2 4
8 7 6 7
Akzeptierte Antwort
Weitere Antworten (1)
Or Shem Tov
am 2 Apr. 2020
0 Stimmen
5 Kommentare
Or Shem Tov
am 2 Apr. 2020
Or Shem Tov
am 2 Apr. 2020
Adam Danz
am 2 Apr. 2020
I just updated my answer to show that the answer works with your data. I don't know why you are getting those errors. My guess is that a variable was overwritten in your workspace. Please see the updated section of the answer.
Or Shem Tov
am 2 Apr. 2020
If you look at my updated answer, it does work. The problem was that this line
C(cellfun(@isempty, C)) = {table(nan, nan, nan, nan, nan)};
correctly filled the cell array with a NaN table but the variable names did not match the other tables' variable names. Since it didn't have the same variable names as the other tables, they couldnt' be vertically concatenated. Note that this produces a different error than the ones you shared.
If you look at my updated answer, it works with the data you provided. If you'd rather use 1s than NaNs, you can replace the NaNs in that line of code.
Kategorien
Mehr zu Data Preprocessing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

