How to write a .txt file in this way?
이전 댓글 표시

I am writing a code like this
if D == 0
d0_L2_pre = norm(v).^2;
else
d0_L2_pre = norm(v).^2/abs(D);
endI want to store the results(d0_L2_pre) in a .txt file like the way it is showing in the picture. I was trying
LastName = {'M-01';'M-02';'M-03';'M-04';'M-05';'M-06';'M-07';'M-08';'M-09';'M-10';'M-11';'M-11'};
d0_L2_Pre = [d0_L2_pre];
T = table(d0_L2_Pre,'RowNames',LastName);
writetable(T,'distance.txt','WriteRowNames',true)
type 'distance.txt'
And then how to save the .txt file in certain directory?
How to edit or write a new code which will write a .txt file just like the picture.
채택된 답변
추가 답변 (1개)
ANKUR KUMAR
2018년 10월 1일
name={'pre','post','shift'}
A={rand(1,12),rand(1,12),rand(1,12)} %taking a random data for pre, post and shift
for kk=1:3
column1=arrayfun(@(x) strcat('"M',num2str(x),'"'),1:12,'uni',0)';
column2=arrayfun(@(x) num2str(x) , A{kk},'uni',0)';
tab{kk}=[{strcat('d0_Ld2_',name{kk}),''};[column1 column2]]
end
table=cat(1,tab{:})
dlmcell('sample.txt',table)
Get the dlmcell function from https://in.mathworks.com/matlabcentral/fileexchange/25387-write-cell-array-to-text-file?focused=3804347&tab=function
댓글 수: 7
ANKUR KUMAR
2018년 10월 1일
편집: ANKUR KUMAR
2018년 10월 1일
If the above prog seems to be difficult for you, then you can refer this. Both yields the same result.
clc
clear
name={'pre','post','shift'}
%for pre
A=rand(1,12);
column1=arrayfun(@(x) strcat('"M',num2str(x),'"'),1:12,'uni',0)';
column2=arrayfun(@(x) num2str(x) , A,'uni',0)';
tab1=[{'d0_Ld2_pre',''};[column1 column2]]
%for post
A=rand(1,12);
tab2=[{'d0_Ld2_post',''};[column1 column2]]
%shift
A=rand(1,12);
tab3=[{'d0_Ld2_shift',''};[column1 column2]]
table=[tab1;tab2;tab3];
dlmcell('sample.txt',table)
ANKUR KUMAR
2018년 10월 2일
Accept the answer if it helps you.
ANKUR KUMAR
2018년 10월 4일
The screenshot which you have sent is correct. Your matlab is also showing is the location of every elements. Open that variable tab1 by typing open tab1.
ANKUR KUMAR
2018년 10월 4일

ANKUR KUMAR
2018년 10월 4일
dlmcell('sample.txt',tab1)
Save this output and open the text file. And send us the screenshot of text file too.
ANKUR KUMAR
2018년 10월 4일
See the output file, resulted from the very first code.
카테고리
도움말 센터 및 File Exchange에서 Signal Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

