Dynamic vectors into cell

조회 수: 2 (최근 30일)
Juan Medved
Juan Medved 2020년 7월 31일
댓글: Juan Medved 2020년 8월 1일
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
Juan Medved
Juan Medved 2020년 7월 31일
Thanks for the answer!!
And how can i manage a table(from the GUI) with 2 columns of numbers and 1 column of logical data?
dpb
dpb 2020년 7월 31일
편집: dpb 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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 7월 31일
편집: Stephen23 2020년 7월 31일
It is very easy to get what you want, you just need to avoid concatenating all of the numeric/logical data together, e.g.:
>> D = [num2cell(A(:)),num2cell(B(:)),num2cell(C(:))]
D =
[1] [2] [1]
[1] [2] [0]
[1] [2] [1]
And checking:
>> MyCorrectData={1 2 true; 1 2 false; 1 2 true}; %this is what i want
>> isequal(D,MyCorrectData)
ans =
1
For an arbitrary number of arrays just put them into the cell array first, e.g.:
>> D = {A,B,C}; % any number of suitably-sized arrays.
>> D = cellfun(@(a)num2cell(a(:)),D,'uni',0);
>> D = [D{:}] % horzcat
D =
[1] [2] [1]
[1] [2] [0]
[1] [2] [1]
  댓글 수: 1
Juan Medved
Juan Medved 2020년 8월 1일
Thank you!!!!
This works fine!!!!!!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by