Save a .dat file containing words and values

3 次查看(过去 30 天)
I know this topic has already been addresses, however none of the answers found in other questions could help me.
My problem is that I need to open a .dat file, change some of its values, and save it back. Probably the content in it of both characters and values makes it problematic to save it.
The save or dlmwrite do not work, and writematrix is not supported.
Does anyone know another way on how to save the file?
Thank you.
I attach the code I am using, the .txt from which I get the values to replace the existing ones, and a .txt version of the .dat file I have, since I cannot upload the original due to non supported file extension.
case_path='H:\Desktop\Samples\CASES\';
Samples=load(['Cases.txt']);
%%
N_an=10;
for ii=1:N_an
inp= textread([case_path,num2str(ii),'\INPUT SILENCE.dat'],'%s%*[^\n]');
inp{4}=num2str(0.045);
inp{21}=num2str(Samples(ii,1));
inp{22}=num2str(Samples(ii,2));
inp{23}=num2str(Samples(ii,3));
inp{26}=num2str(Samples(ii,3));
inp{27}=num2str(Samples(ii,4));
inp{28}=num2str(Samples(ii,6));
inp{88}=num2str(Samples(ii,7));
inp{89}=num2str(Samples(ii,8));
inp{90}=num2str(Samples(ii,9));
save([case_path,num2str(ii),'\INPUT SILENCE.dat'],'inp','-ascii');
dlmwrite([case_path,num2str(ii),'\INPUT SILENCE.dat'], inp)
end

采纳的回答

Mathieu NOE
Mathieu NOE 2021-11-15
hello
code modified for my testing purposes...
% case_path='H:\Desktop\Samples\CASES\';
case_path=pwd;
Samples=load(['Cases.txt']);
% N_an=10;
% for ii=1:N_an
ii=1
% inp= textread([case_path,num2str(ii),'\INPUT SILENCE.txt'],'%s%*[^\n]');
inp= textread('INPUT SILENCE.txt','%s%*[^\n]');
inp{4}=num2str(0.045);
inp{21}=num2str(Samples(ii,1));
inp{22}=num2str(Samples(ii,2));
inp{23}=num2str(Samples(ii,3));
inp{26}=num2str(Samples(ii,3));
inp{27}=num2str(Samples(ii,4));
inp{28}=num2str(Samples(ii,6));
inp{88}=num2str(Samples(ii,7));
inp{89}=num2str(Samples(ii,8));
inp{90}=num2str(Samples(ii,9));
% save([case_path,num2str(ii),'\INPUT SILENCE out.txt'],'inp','-ascii');
% dlmwrite([case_path,num2str(ii),'\INPUT SILENCE out.txt'], inp)
writecell(inp,'INPUT SILENCE out.txt')
% end
it works if you save the new file with writecell (instead of save or dlmwrite)
tested with an output filename different so I could check the modifications of the files
now the leadings and trailing blanks are lost and I don't know if it's important for you.
  2 个评论
Giorgio Caumo
Giorgio Caumo 2021-11-16
Thank you really much!
In the computer where I have my main files unfortunately Matlab is still at the old version so doesn't recognize writecell, however I tried on my personal which is updated and it works. I will upload the necessary things from that.
Moreover the dat file that is generated gets recognized by the software that needs to read it.
I had tried the fopen/fclose procedure too, and although the dat file looks with the correct values, probably a format problem do not allow the software to recognize the file generated this way.
Thank you again!

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by