Mat structure to csv

조회 수: 63 (최근 30일)
N/A
N/A 2023년 1월 24일
댓글: dpb 2023년 1월 24일
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
dpb
dpb 2023년 1월 24일
Too little info and no go to external site...see <Another Similar Q?>
the cyclist
the cyclist 2023년 1월 24일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2023년 1월 24일
편집: the cyclist 2023년 1월 24일
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
the cyclist
the cyclist 2023년 1월 24일
Here is what is stored in the structure.
No, it is not possible to just send the whole structure to Excel.
Yes, you can write code to parse out each field, and send individual fields to Excel.
TC will be tricky, because it is a 3-dimensional array. You might need to send it to 214 Excel files.
dpb
dpb 2023년 1월 24일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by