How to write a mixed data (number and {real, imag}) in a CSV file ?

1 view (last 30 days)
I need to write a CSV file with matrix in the first column and {real, imag} in the second column.
Example
matrix1 = {10;20;30};
matrix2 = [ { 2.3027900000000e-03, 2.0292300000000e-11};{ 8.6489600000000e-03, 1.7520000000000e-03};{ 8.5228400000000e-03, 7.1978700000000e-04}];
CSV file output as:
10 { 2.3027900000000e-03, 2.0292300000000e-11}
20 { 8.6489600000000e-03, 1.7520000000000e-03}
30 { 8.5228400000000e-03, 7.1978700000000e-04}
The second is real and imag value in curly brackets with a comma. I need this format to replace the data in a pyhond code.
Thanks!

Accepted Answer

Voss
Voss on 6 Dec 2022
Does this work for you?
matrix1 = {10;20;30};
matrix2 = [ { 2.3027900000000e-03, 2.0292300000000e-11};{ 8.6489600000000e-03, 1.7520000000000e-03};{ 8.5228400000000e-03, 7.1978700000000e-04}];
C = [matrix1 compose('{%e, %e}',reshape([matrix2{:}],size(matrix2)))];
writecell(C,'csv_file.csv')
If not, please upload a sample csv file that has the required format.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!