how to save values to text file in specific format

2019-09-19 00:33发布

问题:

I have a matrix of values like [150 255 25;400 80 10;240 68 190]. I want to store these values to text file in hexadecimal format such that each value in matrix is represented by 3digit hexa value (12bit). i.e

Decimal Hexa notation 150 255 25 096 0FF 019 400 80 10 -> 190 050 00A 240 68 190 0F0 044 0BE

I am using like this

`fp=fopen('represen.dat','wb');
for i=1:1:x
   for j=1:1:y
       fprintf(fp,"%3x\t",A(i,j));
   end
   fprintf(fp,"\n");
end`

It is giving result as Decimal Hexa notation 150 255 25 96 FF 19 400 80 10 -> 190 50 0A 240 68 190 F0 44 BE

help me in this regard..

回答1:

To insert leading zeros your fprint command should look like this:

fprintf(fp,"%03x\t",A(i,j));


标签: matlab format