Mat structure to csv

7 views (last 30 days)
N/A
N/A on 24 Jan 2023
Commented: dpb on 24 Jan 2023
I have a mat structure and I need to convert it to csv. I tried writecsv, but it gives me an empty csv. Could you give me some ideas? The file is from page: https://zenodo.org/record/2654460#.Y8_6h3bMKF5, for 2015. Thanks.
  2 Comments
the cyclist
the cyclist on 24 Jan 2023
The info in the question you linked to reminded me that there is also the writestruct function, that will write a structure directly to file. However, the structure here doesn't meet the internal data requirements for that function to operate on it.

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 24 Jan 2023
Edited: the cyclist on 24 Jan 2023
I downloaded the first file on that page, which is EXIOBASE_3rx_aggLandUseExtensions_1995_pxp.mat, and then uploaded it to MATLAB.
The contents of that file is two structure arrays. Each of these structure arrays contains multiple fields (as described in that documentation link).
Some of those fields are 2-dimensional arrays (that could be saved to CSV files). But some are 3-dimensional arrays, which you would need code to parse and save.
There is no trivial way to just save everything as a CSV.
  4 Comments
dpb
dpb on 24 Jan 2023
From
doc writematrix
...
Algorithms
There are some instances where the writematrix function creates a file that does not
represent the input data exactly. You will notice this when you use the readmatrix
function to read that file. The resulting data might not have the exact same format
or contents as the original array. If you need to save your array and retrieve it
at a later time to match the original array exactly, with the same data and organization,
then save it as a MAT-file. writematrix writes inexact data in the following instances:
writematrix writes out numeric data using long g format, and categorical or character
data as unquoted text.
writematrix writes out arrays that have more than two dimensions as two
dimensional arrays, with the trailing dimensions collapsed.
What does this mean, exactly...let's create a simple example 3D array with 1,2,3 on each 2x2 plane...
>> M=ones(2).*cat(3,1,2,3);
>> writematrix(M,'M.csv','FileType','text')
>> type M.csv
1,1,2,2,3,3
1,1,2,2,3,3
>>
You see it wrote the 2x2 arrays concatenated together on two lines/records, the size(M,1).
So, the TC array would be 214 (3rd dimension) 200x214 2D arrrays written horizontally.
What would OP consider should be the format of higher-dimension arrays in a text file? He doesn't provide any guidance in what even would expect the result to be.
Alternatively, of course, one could do as @the cyclist says and parse the array and write each plane to a sheet in a given workbook if were to use Excel; a .csv file doesn't have any concept of sheets.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!