Convert struct to double type in Matlab

前端 未结 1 1012
野的像风
野的像风 2021-01-15 02:23

Glcm (feature extraction method) give me an output in \'struct\' type, while i need the output in \'double\' type. I need a \'double\' type as the input variable for the nex

相关标签:
1条回答
  • 2021-01-15 02:58

    First of all, rather than first converting to a cell and then to a matrix, you can convert directly from a struct to a double using struct2array.

    fetrain = struct2array(gl);
    

    That aside, there is no difference between a "complex double" and a double. They are both of type double.

    class(1i)
    % double
    

    You can use real to get the real component of the complex number or abs if you need the magnitude of it.

    real(1+1i)
    %   1
    
    abs(1+1i)
    %   1.4142
    

    In your case this would be:

    fetrain_real = real(fetrain);
    fetrain_mag = abs(fetrain);
    

    Update

    By default struct2array concatenates the data horizontally. If you want your data to be a matrix that is nFields x nData, then you could do something like the following:

    fetrain = struct2array(gl);
    
    % Reshape to be nFields x nData
    fetrain = permute(reshape(fetrain, [], numel(fieldnames(gl)), numel(gl)), [2 1 3]);
    
    0 讨论(0)
提交回复
热议问题