4
votes

I'm trying to convert a .mat file into csv, preserving the vector/variable names. This is one example of what I'm dealing with:

mymat =

       model_id: [2217x1 double]
    own_dummies: [2217x26 double]
             id: [2217x1 double]
        product: [2217x1 double]
          const: [2217x1 double]
            mpd: [2217x1 double]
            air: [2217x1 double]
            mpg: [2217x1 double]
          trend: [2217x1 double]
          space: [2217x1 double]
           hpwt: [2217x1 double]
        cdindex: [20x1 double]
           cdid: [2217x1 double]
         outshr: [2217x1 double]
         firmid: [2217x1 double]
          share: [2217x1 double]
          price: [2217x1 double]

I've tried using csvwrite('test.csv', mymat) but it gives me an error:

??? Undefined function or method 'real' for input arguments of type 'struct'.
Error in ==> dlmwrite at 192
                str = sprintf('%.*g%+.*gi',precn,real(m(i,j)),precn,imag(m(i,j)));\
Error in ==> csvwrite at 32
dlmwrite(filename, m, ',', r, c);

I guess the problem is that I'm feeding csvwrite with a struct instead of a matrix. I can convert the struct variable to variable to a matrix, but then I would loose the variable names.

Surely there is a better way?

1
Where would you save the variable names then? Do you want the them to be stored in the first column? - Eitan T
Hi, OP here. Yes, first column or first row would do - cd98

1 Answers

2
votes

how would you actually map the struct to a csv? you cannot do a straight forward map of fieldnames as entries on first line, because the matrices have incompatible dimensions. So this example you'd have to 1. introduce several columns for own_dummies and 2. expand the cdindex.

So you could write a wrapper to make the matrices themselves compatible and to write the columns yourself into the file. something along the (untested,conceptual) lines of

    function saveData(filename, data, type)

    % at first bring struct data to sensible format
    if strcmp(type)='my_mat_type'
        data.own_dummies2 = data.own_dummies[2,:]; % split into seperate columns
        %...
        data.own_dummies26 = data.own_dummies[26,:];
        data.own_dummies = data.own_dummies[1,:];
        data.cdindex = [data.cdindex -ones(1,2217-length(data.cdindex)]; % pad any missing values as -1
    end;


    FD = fopen(filename, 'w');

    %todo did it open?

    fields = fieldnames(data);
    nfields = length(fields);

    % create column name values
    columns = strcat(strcat(fields,',')); % creates string = col1,col2,...coln,
    columns = columns(1:length(columns)-1); %remove trailing comma

    CRNL = char([10 13]); % or so

    % print columns and newline
    fprintf(FD,strcat(columns,CRNL)); 

    dataout = cell2mat(struct2cell(data));

    % use your method to write the data to the file

    fclose(FD);

naturally, you could also just split the individual fields into separate files where the fieldname is contained within the filename, which might be simpler, generally speaking.