MATLAB Answers

How can I format a large cell of data into one big column underneath a (predeclared) title?

2 views (last 30 days)
My goal is to generate some kind of 'tabular' representation of data and then output this filtered dataset to a new file. Something that looks like:
Foo Bar Fubar
0 0 0
1 100 50
................(54321 lines later)
.......................................
54321 0 1441
The only way I can obtain the data though is through a 1x1 struct containing 2 regions of 155x1 cell, 1 called titles, 1 called data.
So, what I do is...
titles = transpose(theStruct.theTitles(index));
*Index is the already known set of indexes I'm pulling Foo/Bar/Fubar titles from
my_storage = titles; %foo, bar, fubar header titles
my_storage{2,1} = transpose(theStruct.theData{6,1}); %foo data
my_storage{2,2} = transpose(theStruct.theData{7,1}); %bar data
my_storage{2,3} = transpose(theStruct.theData{8,1}); %fubar data
What I end up getting though is just the cell arrays containing all doubles in 'theData{6,1}'/'theData{7,1}' in 'my_storage', so it looks like:
Foo Bar Fubar
54321x1 double 54321x1 double 54321x1 double
Foo, Bar, and Fubar are ALL cells as well too, which seems like it 'might' be problematic to write to files with.
I played around with 'my_storage(:,1)' (colon operator) to try to modify it to write its contents to all data underneath, but it didn't work based off what I tried.
How could I have the entire cell underneath Foo, Bar, and Fubar (the 54321x1 double cell) turned into just a large column of data? Is there a built-in, simple method that doesn't involve a raw for-loop?
Thanks!

Accepted Answer

Shubham Gupta
Shubham Gupta on 7 Nov 2019
Edited: Shubham Gupta on 7 Nov 2019
Try this:
my_storage = titles; %foo, bar, fubar header titles
my_storage_mat = [transpose(theStruct.theData{6,1}),... % foo
transpose(theStruct.theData{7,1}),... % bar
transpose(theStruct.theData{8,1})]; % fubar
my_storage_cell = mat2cell(my_storage_mat,ones(size(my_storage_mat,1),1),ones(size(my_storage_mat,2)),1);
my_storage = [titles;my_storage_cell];
You should get what you need, let me know if you have doubts.

  2 Comments

Lance Raju
Lance Raju on 7 Nov 2019
Thank you, I seem to be getting a neater matrix, but I get an error with the line:
my_storage_cell = mat2cell(my_storage_mat2, ones(size(my_storage_mat,1),1),ones(size(my_storage_mat,2)),1);
It says: 'Error using mat2cell
Input arguments, D1 through D2, should be vectors.'
I'm a bit confused as to what this means
Shubham Gupta
Shubham Gupta on 7 Nov 2019
I am sorry I made error with the brackets position. Try this:
my_storage_cell = mat2cell(my_storage_mat,ones(size(my_storage_mat,1),1),ones(size(my_storage_mat,2),1));

Sign in to comment.

More Answers (0)