필터 지우기
필터 지우기

Saving in txt format in Matlab with commas and semicolons

조회 수: 8 (최근 30일)
MRC
MRC 2015년 1월 20일
댓글: Image Analyst 2015년 1월 20일
I have a huge matrix in Matlab that I want to save in .txt format (or in any other text format).
Suppose the matrix is
A =
1 2 3
4 5 6
7 8 9
If I type
save prova.txt A -ASCII
I get the matrix in .txt format as
1 2 3
4 5 6
7 8 9
(in an horrible exponential form, actually)
I would like to get instead
1, 2, 3;
4, 5, 6;
7, 8, 9;
Can you help me? In addition, do you know a way to make the exponential form disappear?

답변 (1개)

Image Analyst
Image Analyst 2015년 1월 20일
편집: Image Analyst 2015년 1월 20일
Use
fid = fopen(filename, 'wt');
if fid ~= -1
for row = 1 : size(A, 1);
fprintf(fid, '%d, %d, %d;\n', A(row, 1), A(row, 2), A(row, 3));
end
fclose(fid);
end
  댓글 수: 2
MRC
MRC 2015년 1월 20일
편집: MRC 2015년 1월 20일
What's filename in my case? Should I save before?
I can't type explicitly each row of A because it is a huge matrix in reality.
Image Analyst
Image Analyst 2015년 1월 20일
You can pick whatever filename you want.
You must be saving this as a text file. If it was a binary file or a .mat file then you wouldn't care at all about commas and semcolons because you would not see them at all.
If A has a huge number of columns, you can do
outputString = sprintf('%d, ', A(row, :));
% Get rid of final command and space and add a semicolon instead
fprintf('%s;', outputString(1:end-2));

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by