Converting Table with matrices to CSV

14 ビュー (過去 30 日間)
Eren
Eren 2022 年 12 月 5 日
回答済み: Eren 2022 年 12 月 5 日
Hi,
I have been trying to convert a .mat file to .csv, but the table I've been working with has {nxm double} matrices in it.
I tried the 'writetable()' function, but it "one-hot encodes" the whole thing.
For example when I run it I get:
title data
_____________ ___________________
"A" {2×4 double }
"B" {5×4 double }
"C" {2×4 double }
"D" {[000 001 002 003]}
"E" {[004 005 006 007]}
but, the .csv file splits the matrix into "data_1", "data_2" ... "data_n".
I also tried using:
writetable(X,'X_test.csv','Delimiter',',','QuoteStrings',true)
type 'X_test.csv'
to write the matrix as a string, but it didn't make a difference.
I want the output of the matrices to be like python lists, for example, for a {2x4 double} matrix:
[[734, 234, 987, 174], [193, 297, 379, 222]]
or any other way where it is not split into different columns.
Thanks in advance!
  3 件のコメント
Eren
Eren 2022 年 12 月 5 日
How would I apply that to every row of a column?
Walter Roberson
Walter Roberson 2022 年 12 月 5 日
jsonencode() maybe?

サインインしてコメントする。

採用された回答

Eren
Eren 2022 年 12 月 5 日
Ok, I found a solution. It doesn't convert it to csv, it converts it to json, which I can converted to csv in python.
js = jsonencode(Table)
T=['x=2;',newline,'y=3;',newline,'z=x*y;'];
filename = 'myTextFile.json';
fid = fopen(filename,'w');
fprintf(fid,'%s',js);
fclose(fid);
open(filename)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by