How can I combine data from multiple cells into one matrix?

6 vues (au cours des 30 derniers jours)
Katharine Wunsch
Katharine Wunsch le 21 Mar 2016
I have a cell that contains 233 matrices, each with data in it. I would like to pull all of this data into one matrix with 23 columns, however I run into problems because each matrix does not have the same number of rows, though they all have 23 columns. This code pulls most of the data into the matrix, but still overwrites some lines when multiple columns are being added from the same matrix.
load('initialdata.mat'); %1x233 cell
k=length(initial data);
for i=1:k
finaldata = zeros(23,500); %500 is arbitrary
m=1
z=0
for j = 1:500
j = m
[row column] = find(timecell{1,j});
if length(row) == 23
z= 1
for i = 1:23
finaldata(i,j) = (timecell{1,j}(1,i));
end
elseif length(row) > 23
for z = 1:length(row)/23
for i = 1:23
finaldata(i,j+z) = (timecell{1,j}(z,i));
end
end
elseif length(row) == 0;
z= 0
end
m = j+z
end
end
Thank you!

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 21 Mar 2016
A={rand(4,23) rand(5,23) rand(10,23)}
B=cell2mat(A')

Plus de réponses (1)

Image Analyst
Image Analyst le 21 Mar 2016
What is your cell array called? "initial data" is not a valid variable name so don't know how that got past the length() call. Is it timecell? Did you try something like
finaldata = timecell{:};
If that doesn't work, maybe try transposing timecell. Otherwise go down cell by cell concatenating with a semicolon
for k = 1 : length(timecell)
if k == 1
finaldata = timecell{k};
else
finaldata = [finaldata; timecell{k}];
end
end
  1 commentaire
Katharine Wunsch
Katharine Wunsch le 21 Mar 2016
Oops, yes it is timecell, not initial data. Transposing worked, thank you!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by