Dynamic vectors into cell
显示 更早的评论
Hi!
I have 3 vectors (here A,B and C) that could have diferent size (are dynamics, size is not define) and i need to make a cell like "MyCorrectData" where the last column ist logical.
A=[1 1 1]
B=[2 2 2]
C=logical([1 0 1])
MyCorrectData={1 2 true; 1 2 false; 1 2 true}; %this is what i want
This works almost fine, but the last column lost the type and convert in double
A=[1 1 1]
B=[2 2 2]
C=logical([1 0 1])
MyCorrectData={1 2 true; 1 2 false; 1 2 true}; %this is what i want
MyData=[A B C]
MyData=num2cell(MyData)
How can i solve this?
Thanks!
3 个评论
"MyCorrectData={1 2 true; 1 2 false; 1 2 true}; %this is what i want"
"It's a cold, cruel world when one can't always have what one wants."
You simply can't mix data types in an array; you can have a cell array in which different elements in the cells contain differing data types, but not in a single cell.
About closest you could come to your above would be
M={[1 1 1];
[2 2 2];
logical([1 0 1])};
yields
>> M
M =
3×1 cell array
{1×3 double }
{1×3 double }
{1×3 logical}
>>
Juan Medved
2020-7-31
Columns of tables can be of differing types...you just store what you want.
Never used the uitable in anger, dunno if it will allow cell input by table cell to mix random types or not. The table class must be the same type for the column if not a cell but will allow a cell so can mix. Generally if do so it gets much more difficult to write generic code, however, as then have to handle the cells differently depending upon content.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!