Save a .dat file containing words and values
7 views (last 30 days)
Show older comments
Giorgio Caumo
on 13 Nov 2021
Commented: Mathieu NOE
on 18 Nov 2021
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 Comments
Accepted Answer
Mathieu NOE
on 15 Nov 2021
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 Comments
More Answers (0)
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!