Search code examples
arraysmatlabstructcellscell-array

convert struct to cell array indexes Matlab


How to convert struct to cell array indexes, I have struct which I want to convert to the cell array indexes with respect to to data & label. Or possibly direct cell arrays according to label and data to avoid struct usage.

My Code:

for ii = 1:13
    for jj = 1:numel(obj.R)
        %R calc operatioiins
    end
    R=R(~cellfun('isempty',R)) ;
    TData(ii).data=R;
    TData(ii).label=cellfun(@(x) ['T' num2str(ii)] , num2cell(1:length(R))', 'UniformOutput', false);

end

OUTPUT:

  TData(1).data

ans = 

    [10x3 double]
    [10x3 double]
    [10x3 double]
    [10x3 double]
    [10x3 double]
    [10x3 double]
    [10x3 double]

>> TData(1).label

ans = 

    'T1'
    'T1'
    'T1'
    'T1'
    'T1'
    'T1'
    'T1'

EDIT 1:

Possibly change the following: Actual Output:

[10x3 double]
[10x3 double]
[ 8x3 double]
[10x3 double]
[10x3 double]
[10x3 double]
[10x3 double]
'T1'         
[ 5x3 double]
[ 5x3 double]
[ 5x3 double]
[ 5x3 double]
[ 5x3 double]
[ 5x3 double]
'T2'         
[ 5x3 double]
[ 5x3 double]
[ 5x3 double]
[ 5x3 double]
[ 5x3 double]
[ 5x3 double]
'T3'         

Desired Output:

[10x3 double] 'T1' 
[10x3 double] 'T1' 
[ 8x3 double] 'T1' 
[10x3 double] 'T1' 
[10x3 double] 'T1' 
[10x3 double] 'T1' 
[10x3 double] 'T1' 

Solution

  • I'm not 100% on this, but try adding this to the end of your for-loop:

    TDataCellArray{ii} = {{TData(ii).data}, {TData(ii).label}}