SJDS
SJDS

Reputation: 1289

MATLAB export data stored in a double array and cell array to a CSV file

I have a MATLAB structure with 19 fields. The main field is a 1 x 108033 double with all values numeric. It looks like this, basically 108033 numbers:

pnum: 5384940 5437561 5570271 5661637 5771155   ...

I have another field called inventors which is a 1 x 108033 cell value. Every cell contains a different number of strings. Columns 1 to 5 for example are

inventors:  {2x1 cell}    {4x1 cell}    {1x1 cell}    {1x1 cell}    {1x1 cell}

For the first column value, the 2 x 1 cell consists of the following values 5012491-01 and 2035147-03 and so on.

I'd like to jointly export these two to a CSV file. The ideal outcome would repeat the number in pnum so that it establishes a clear link between the pnum and the inventors. Thus, the ideal outcome would look something like this (with the contents of what is in the inventors cell displayed).

pnum    inventors
5384940 5012491-01
5384940 2035147-03
5437561 5437561-01
5437561 5437561-02
5437561 5437561-03
5437561 5012491-02
5570271 5437561-03
5661637 1885634-08
5771155 5012491-01

I asked a more complex version of this question before but it was not clear enough what the problem was. Hope it is now.

Upvotes: 1

Views: 389

Answers (1)

rayryeng
rayryeng

Reputation: 104464

I'm assuming each cell in inventors is a cell array of strings. It wouldn't make sense for these to be actual floating point or intenger numbers, because the dash would subtract the two numbers separating them together. Now, because you're writing to a CSV file, the easiest thing I can think of is to iterate over each number and cell, then repeat the ID number for as many times as there are elements in a cell. First create the right headers, then write your results. Something like this comes to mind:

f = fopen('data.csv', 'w'); %// Open up data for writing
fprintf(f, 'pnum,inventors\n'); %// Write headers
for ii = 1 : numel(pnum) %// For each unique number
    inventor = inventors{ii};
    for jj = 1 : numel(inventor) %// For each inventor ID
        fprintf(f, '%d,%s\n', pnum(ii), inventor{jj}); %// Write the right combo to file
    end
end
fclose(f); %// Close the file

fopen here opens up a file called data.csv so we can write things to it. What is returned is a file pointer called f, which we use to write stuff to this file. After, we write the headers of the file, consisting of pnum and inventors. This is a CSV file so there's a comma separating the two. Now, for each unique number, we then access the right slot in inventors then for each unique inventor, add the same unique ID with the right inventor ID as a line in this file. I use fprintf to write things to file using the associated file pointer established earlier. Once we're done, close the file with fclose.


To double check that this works, I've used the small example you've provided in your post:

pnum = [5384940 5437561 5570271 5661637 5771155];

inventors = {{'5012491-01', '2035147-03'}.', {'5437561-01', '5437561-02', '5437561-03', '5012491-02'}.', {'5437561-03'}, {'1885634-08'}, {'5012491-01'}};

Bear in mind that I don't have access to your struct, so you'll have to access the right fields and assign them to the corresponding variables seen above. So if your struct is called something like data, then you'd do this before you run the above code:

pnum = data.pnum;
inventors = data.inventors;

Running the above code I just wrote and opening up the CSV file (which is called data.csv), I get this:

pnum,inventors
5384940,5012491-01
5384940,2035147-03
5437561,5437561-01
5437561,5437561-02
5437561,5437561-03
5437561,5012491-02
5570271,5437561-03
5661637,1885634-08
5771155,5012491-01

Upvotes: 1

Related Questions