I need to make an Excel file (or CSV) with a list of variable names in the first column (e.g. attached "Nm.mat" file), and their respective value in the second column. The values are a mix of scalars and matrices (e.g. attached "Val.mat" file). I used the following command to generate the csv file after loading Nm and Val to the workspace:
>> writetable(table(Nm,Val),'test.csv','WriteVariableNames',false);
Unfortunately, it writes all the values from the 12x12 matrix in a long row array with one value per cell (see "test_is.csv"). I would like it to arrange them all into a single cell as in the "test_shouldbe.csv" file.
Please help me generate the "test_shouldbe.csv" file. It's fine if it writes it to an Excel instead as that's ultimately what it's going to be. Thanks in advance!

 채택된 답변

Voss
Voss 2022년 3월 21일

1 개 추천

load('Nm.mat');
load('Val.mat');
t = table(Nm,Val);
% make a cell array containing the table's contents
C = table2cell(t);
% convert the second column to character vectors representing the matrices
C(:,2) = cellfun(@mat2str,C(:,2),'UniformOutput',false)
C = 2×2 cell array
{'Name1'} {'0' } {'Name2'} {'[1 -1 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 1 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0]'}
% write the cell array to file
writecell(C,'test_shouldbe.csv');

댓글 수: 4

Bastian Kindereit
Bastian Kindereit 2022년 3월 21일
Hello,
Thanks for the very prompt response! The output is still on a single line for me, albeit separated by semicolons to indicate a new row. Is there any way to get it to actually go to a new line? Thanks again
I see now. I wasn't able to see the complete contents of the cell in test_shouldbe.csv before.
You can try the following. (The resulting csv for the sample data you shared is attached, as "test_shouldbe.csv".)
load('Nm.mat');
load('Val.mat');
t = table(Nm,Val);
% make a cell array containing the table's contents
C = table2cell(t);
% convert the second column to character vectors representing the matrices
C(:,2) = cellfun(@mat2str_multi_line,C(:,2),'UniformOutput',false)
C = 2×2 cell array
{'Name1'} {'"0"' } {'Name2'} {'"[1 -1 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 1 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0]"'}
% write the cell array to file
writecell(C,'test_shouldbe.csv');
% check the file (have to specify ('Delimiter', ',') to get it to read right)
readcell('test_shouldbe.csv','Delimiter',',')
ans = 2×2 cell array
{'Name1'} {'"0"' } {'Name2'} {'"[1 -1 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 1 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0↵0 0 0 0 0 0 0 0 0 0 0 0]"'}
function s = mat2str_multi_line(M)
s = ['"' strrep(mat2str(M),';',sprintf(newline())) '"'];
end
Bastian Kindereit
Bastian Kindereit 2022년 3월 22일
Wow, thanks so much, that worked perfectly!
Voss
Voss 2022년 3월 22일
Excellent! You're welcome!

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

추가 답변 (0개)

카테고리

제품

릴리스

R2022a

질문:

2022년 3월 21일

댓글:

2022년 3월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by