write in text file
Show older comments
I would like to write a string of hex numbers to a text file. What I want in the file is like this:
00000000 00000000......00000000 00000000
1111111F 10123BD9......34GA0342 06FFFFFF
...
i.e. every 8 numbers separated by some space and every row shall contain 256 such groups of numbers. My code is the following:
fileID = fopen('text.txt','w');
z=0;
for i=1:8:1024*2
fprintf(fileID,'%s \t',A_HEX(i:i+8-1));
z=z+1;
if z>=256
fprintf(fileID,'%s \n','');
z=0;
end
end
fclose(fileID);
However,in the text file it looks like a mess-up. I attached the 'test.txt' below.
1 Comment
Walter Roberson
on 5 Dec 2016
'G' in a hex output would indeed be really messed up :)
Answers (2)
One fast solution is to not use a loop:
str = dec2hex(randi([0,15],1,5000));
fmt = repmat(sprintf('%s\t',repmat('%c',1,8)),1,256);
fmt = sprintf('%s\n',fmt(1:end-1));
fid = fopen('temp0.txt','wt');
fprintf(fid,fmt,str)
fclose(fid)
Jan
on 5 Dec 2016
fmt = [repmat('%c%c%c%c%c%c%c%c ', 1, 256), char(10)];
fid = fopen('temp0.txt', 'W');
fprintf(fid, fmt, A_HEX);
fclose(fid)
Categories
Find more on Text Files 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!