Is it possible to add initial lines to a cell as if they were headings?
For example: given a cell of double 5x3 is it possible to add a line where the fields are strings?

 Accepted Answer

Star Strider
Star Strider on 16 Mar 2019
Yes. Use Tables (link).

6 Comments

Can you post an example please?
Certainly.
I am not certain what you are starting with. That affects how the table (link) is created and addressed.
Try this:
C = {rand(5,3)}; % ‘cell Of double 5x3’
C3 = mat2cell(C{:}, size(C{:},1), ones(1,size(C{:},2)));
T = cell2table(C3, 'VariableNames',{'Column_1','Column_2','Column_3'})
producing:
T =
1×3 table
Column_1 Column_2 Column_3
____________ ____________ ____________
[5×1 double] [5×1 double] [5×1 double]
To recover the data (in this illustration, others may be different):
T1 = T.Column_1{:}
If you want to save it to a file, use the writetable (link) function.
If ‘C’ is a matrix rather than a cell array, the code changes a bit.
thank you
As always, my pleasure.
Sorry, I have another question.
I have, for example, 4 cell. How can I create a struct with all these cell??
I am not certain that I understand what you want.
If you want to assign the table to a structure element, try this:
S.T1 = T;
See the documetation on Structures (link) for details.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!