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);
           end

I 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.

 채택된 답변

Stephen23
Stephen23 2018년 10월 4일
편집: Stephen23 2018년 10월 4일

1 개 추천

Writing that file is easy and efficient with one loop:
C = {'pre','post','shift'};
[fid,msg] = fopen('test.txt','wt');
assert(fid>=3,msg)
for k = 1:numel(C)
D = rand(1,12); % fake data
X = 1:numel(D);
fprintf(fid,'"d0_L2_%s"\n',C{k})
fprintf(fid,'"M%d" %.16f\n',[X(:),D(:)].')
end
fclose(fid);
It generates this file:
"d0_L2_pre"
"M1" 0.8101833471970885
"M2" 0.3228280263956013
"M3" 0.9313125812216142
"M4" 0.0930197953819751
"M5" 0.1431754035600110
"M6" 0.0191395364706218
"M7" 0.3733887218661779
"M8" 0.5956394051737841
"M9" 0.1549615401190287
"M10" 0.2166245210274229
"M11" 0.1375581615241128
"M12" 0.4638896483775406
"d0_L2_post"
"M1" 0.9800029232465585
"M2" 0.6889261537441198
"M3" 0.3944793330174778
"M4" 0.9784100986414408
"M5" 0.7345622798805288
"M6" 0.9344731006203441
"M7" 0.5680906731481710
"M8" 0.3412121838601810
"M9" 0.2602806479751585
"M10" 0.5306524038227418
"M11" 0.3839486545914291
"M12" 0.6184793329342448
"d0_L2_shift"
"M1" 0.4395118376041331
"M2" 0.9157654018281208
"M3" 0.4117131471312308
"M4" 0.1698818550755265
"M5" 0.4333299487649594
"M6" 0.1036376088665931
"M7" 0.7037719110569336
"M8" 0.3658712106700369
"M9" 0.7182559095641131
"M10" 0.0579546739859127
"M11" 0.7510618251853217
"M12" 0.8921570052671490

댓글 수: 8

Mr. 206
Mr. 206 2018년 10월 4일
편집: Mr. 206 2018년 10월 4일
Thank you so much. I am sorry, It is working, I didn't noticed the generated test.txt file. How can save it in certain directory? And how to print like
"d0_L2_pre"
"d0_L2_post"
"d0_L2_shift"
"d1_L2_pre"
"d1_L2_post"
"d1_L2_shift"
Stephen23
Stephen23 2018년 10월 4일
편집: Stephen23 2018년 10월 4일
"How can save it in certain directory?"
F = 'folder where you want the files saved';
...
[fid,msg] = fopen(fullfile(F,'test.txt'),'wt');
"And how to print like..."
fprintf(fid,'"d%d_L2_%s"\n',fix((k-1)/3),C{k})
Mr. 206
Mr. 206 2018년 10월 4일
Here is the output..
Mr. 206
Mr. 206 2018년 10월 4일
The fake data(D) that you use is a vector, whereas in my case my data is coming from some loop, and it is not a simple loop to store all the data in a vector. In this case how can i use D(:)? Is there any other way to feed the value(for example D is coming from several loop) in this case.
And i appreciate your help. Thanks
Stephen23
Stephen23 2018년 10월 4일
편집: Stephen23 2018년 10월 4일
@Atta: it is often easier and more convenient to put data into an array, which is why I used vector D. If your code generates one values at a time, then you can print to the file one line at a time too. Replace the two fprintf calls with this:
fprintf(fid,'"d%d_L2_%s"\n',fix((k-1)/3),C{k})
for ii = ...
val = your calculation which returns one scalar value
fprintf(fid,'"M%d" %.16f\n',ii,val)
end
Mr. 206
Mr. 206 2018년 10월 10일
편집: Mr. 206 2018년 10월 10일
Hi @Stephen I solved the issue regarding 'D(:)', I managed to store them in an array. Thanks.
However as the D value varies from time to time, i modified your code like this. And it creates an empty file in the directory.
here is the code...
Distance_File = '/home/ali/Master_Thesis_Ali/Test_output_02';
C = {'pre','post','shift'};
[fid,msg] = fopen(fullfile(Distance_File,'test.txt'),'wt');
assert(fid>=3,msg)
cc = 0;
for k = 1:numel(C)
cc = cc+1;
if cc == 1
D = d0_L2_pre;
elseif cc == 2
D = d0_p_pre;
else
D = d1_p_pre;
end
X = 1:numel(d0_p_pre);
fprintf(fid,'"d%d_L2_%s"\n\n',fix(k/4),C{k})
fprintf(fid,'"M%d" %.16f\n',[X(:),D(:)].')
end
fclose(fid);
I also tried this
Distance_File = '/home/ali/Master_Thesis_Ali/Test_output_02';
C = {'pre','post','shift'};
[fid,msg] = fopen(fullfile(Distance_File,'test.txt'),'wt');
assert(fid>=3,msg)
for k = 1:numel(C)
if k == 1
D = d0_L2_pre;
elseif k == 2
D = d0_p_pre;
else
D = d1_p_pre;
end
X = 1:numel(d0_p_pre);
fprintf(fid,'"d%d_L2_%s"\n\n',fix(k/4),C{k})
fprintf(fid,'"M%d" %.16f\n',[X(:),D(:)].')
end
fclose(fid);
Stephen23
Stephen23 2018년 10월 10일
"And it creates an empty file in the directory."
Check the size of X and D.
Is the file totally empty, or does it include the header?
Mr. 206
Mr. 206 2018년 10월 10일
Ahha! It was a dimensional mismatch! Thank you so much. It is working now!

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

추가 답변 (1개)

ANKUR KUMAR
ANKUR KUMAR 2018년 10월 1일

3 개 추천

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)

댓글 수: 7

ANKUR KUMAR
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
ANKUR KUMAR 2018년 10월 2일
Accept the answer if it helps you.
Mr. 206
Mr. 206 2018년 10월 4일
편집: Mr. 206 2018년 10월 4일
The output is like this
ANKUR KUMAR
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
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.
Get the dlmcell function from here .
ANKUR KUMAR
ANKUR KUMAR 2018년 10월 4일
See the output file, resulted from the very first code.

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

카테고리

도움말 센터File Exchange에서 Signal Processing Toolbox에 대해 자세히 알아보기

태그

질문:

2018년 10월 1일

댓글:

2018년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by