I have 1500 .txt files containing 5 columns and 71022 rows. I converted them into 1x1500 cell array using import tool. Now the new table data has 1500 tables, each having 5 columns and 71022 rows. I want to have a 3-D matrix. How do I do that?

Antworten (2)

Adam Danz
Adam Danz am 24 Jan. 2019

1 Stimme

Since each matrix is the same size, here's an easy solution: If your cell array is named 'ca', 'm' will be your 3D array.
m = reshape(cell2mat(ca), 71022, 5, 1500);
How this works:
cell2mat() converts your 1x1500 cell into a 71022x(1500*5) matrix.
reshape() converts that matrix into a 3D array with 71022 rows, 5 columns, and 1500 pages.

12 Kommentare

Arup Bhattacharya
Arup Bhattacharya am 24 Jan. 2019
Thanks so much. Now that you've mentioned, the size of the tables inside the array is not same. The number of rows are different. How to proceed?
Adam Danz
Adam Danz am 24 Jan. 2019
If the number of rows differ between the elements of the cell array, you cannot put them into a 3D array. One way around this is to pad the shorter matricies with NaN values (or 0s or something like that) so that all cell elements have the same number of rows. I could help you with that solution if that's what you decide to do. But I urge you to keep the data stored in the cell array since it will be easier to work with.
Arup Bhattacharya
Arup Bhattacharya am 24 Jan. 2019
Help me to get the shorter tables to have the same no. of rows and then convert the whole table of 1x1500 with 1500 tables inside into a 3-d matrix.
Thank you so much.
Adam Danz
Adam Danz am 24 Jan. 2019
Bearbeitet: Adam Danz am 24 Jan. 2019
Continuing from my example above were 'ca' is the cell array of matricies with the same number of columns but varying number of rows,
% get number of rows in each element
nrows = cellfun(@(x)size(x,1), ca, 'UniformOutput', false);
maxRows = max([nrows{:}]);
% Pad each matrix with rows of trailing NaNs
caPad = cellfun(@(x)padarray(x, [maxRows-size(x,1), 0], NaN, 'post'), ca, 'UniformOutput', false);
% Now reshape (same as in my example)
m = reshape(cell2mat(caPad), maxRows, 5, 1500);
The name of my array is 'door1'. celltomat1 is the script where I saved your code. I am getting th efollowing error:
Error using padarray>ParseInputs (line 127)
Function padarray expected A (argument 1) to be numeric or logical for constant padding.
Error in padarray (line 64)
[a, method, padSize, padVal, direction] = ParseInputs(args{:});
Error in celltomat1>@(x)padarray(x,[maxRows-size(x,1),0])
Error in celltomat1 (line 5)
caPad = cellfun(@(x)padarray(x, [maxRows-size(x,1), 0]),door1, 'UniformOutput', false);
Adam Danz
Adam Danz am 24 Jan. 2019
Bearbeitet: Adam Danz am 24 Jan. 2019
If door1 is a cell array of matricies, then door1{1} should produce a matrix. Can you verify that?
Adam Danz
Adam Danz am 24 Jan. 2019
Bearbeitet: Adam Danz am 24 Jan. 2019
...also, I just updated my most recent code to fix a mistake unrelated to the error you shared.
Arup Bhattacharya
Arup Bhattacharya am 24 Jan. 2019
Bearbeitet: Arup Bhattacharya am 24 Jan. 2019
using door1{1} does convert the first table inside door 1 into a matrix. But I want all 1500 of them to be converted into matrices, nested into one 3-D matrix.
Adam Danz
Adam Danz am 25 Jan. 2019
That's exactly what my solution does. I just wanted to confirm that your data were indeed matricies stored in a cell array. But you keep using the word table. Could you attach a mat file that contains your variable 'door1'?
Arup Bhattacharya
Arup Bhattacharya am 25 Jan. 2019
Yes, the data stored in door1 are indeed table, or at least that's what I understood. Matlab is describing those files as 'cell'. For example, I could not square some value referring to a particlular cell inside door1{1,1}.
Right now, I am trying using import tool to import all those 1500 .txt files as a cell array, having 1500 matrices of varying no. of rows and 5 columns. Is that what you told me?
Image Analyst
Image Analyst am 25 Jan. 2019
Maybe there is a better way, like csvread(), importdata(), readtable(), textscan(), etc. where you don't have to mess with the complication of cell arrays.
Someone could probably tell you if you attach your text file.
Adam Danz
Adam Danz am 25 Jan. 2019
My solution works when your variable is a cell array and each element of the cell array is a matrix. I'll need to look at your variable to help further. You could save your variable to a mat file and upload it here.

Melden Sie sich an, um zu kommentieren.

Bob Thompson
Bob Thompson am 24 Jan. 2019

0 Stimmen

You can try using cell2mat(), but I'm not intimately familiar with it enough to know if it will work with arrays inside each cell, or if you need to just have doubles.
Alternatively, you can always just use a loop:
for i = 1:size(cellarray,2)
3dmatrix(:,:,i) = cellarray{i};
end

Kategorien

Produkte

Version

R2018b

Gefragt:

am 24 Jan. 2019

Kommentiert:

am 25 Jan. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by