How to create tab-separated .txt using num2str

조회 수: 12 (최근 30일)
Wei Wen Chen
Wei Wen Chen 2019년 5월 15일
댓글: Adam Danz 2019년 5월 16일
I initially converted some .mat files to tab-separated .txt using dlmwrite, but for some reason the values were not recognized on other platforms, and shows "NaN or non numerical error" instead. I was thinking that num2str could possibly bypass this issue. Here is what I have, but where do I add the tab-separated component?
% Length
load('Lengths.mat')
Len = num2str(DistMatrix)
diary('Lengths.txt')
disp(Len)
diary off
% Weights
load('Weights.mat')
Wei = num2str(CatMatrix)
diary('Weights.txt')
disp(Wei)
diary off
  댓글 수: 2
Adam Danz
Adam Danz 2019년 5월 15일
What is "DistMatrix" ? A matrix? A column vector? A row vector? A numeric scalar? Same with CatMatrix?
Why not use a function that is designed to write to text rather than using diary?
Wei Wen Chen
Wei Wen Chen 2019년 5월 15일
Both DistMatrix and CatMatrix are 16x16 double.
Part of the reason for using diary was just to try something new. The other one being I originally had issues with dlmwrite.

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

채택된 답변

Adam Danz
Adam Danz 2019년 5월 15일
편집: Adam Danz 2019년 5월 16일
If you're using r2019a, use writematrix().
DistMatrix = magic(16); % fake matrix 16x16
fn = 'Lengths.txt'; % file name
writematrix(DistMatrix,fn,'Delimiter','\t') % tab delimiter
Prior to r2019a, you can use this low level function but ~17 decimal places will be recorded.
DistMatrix = magic(16); % fake matrix 16x16
fn = 'Lengths.txt'; % file name
save(fn, 'DistMatrix', '-ascii', '-double', '-tabs') % Save matrix to file with tab delimiter
Or you can use dlmread()
DistMatrix = magic(16); % fake matrix 16x16
fn = 'Lengths.txt'; % file name
dlmwrite(fn,DistMatrix,'delimiter','\t')
Or you can use diary() but I don't recommend this. I'm only including it because it's used in the question.
DistMatrix = magic(16); % fake matrix 16x16
fn = 'Lengths.txt'; % file name
diary on
disp(DistMatrix) %display matrix in command window
diary(fn)
diary off
winopen(fn)
  댓글 수: 2
Wei Wen Chen
Wei Wen Chen 2019년 5월 16일
Will I be able to round the values to the nearest thounsanth by incoporating the round function (or other functions)? I thought about setting the .txt file as variable (A), and do Y = round (A,3), but (A) becomes a 16x16 double again.
Adam Danz
Adam Danz 2019년 5월 16일
I didn't understand your question. Which method are you applying?
Also, I updated my answer to include two more options for matlab releases prior to r2019a.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Equations에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by