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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!