如何使用matlab以特定的格式将值保存到文本文件 [英] how to save values to text file in specific format using matlab

查看:265
本文介绍了如何使用matlab以特定的格式将值保存到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像[150 255 25; 400 80 10; 240 68 190]这样的数值矩阵。我想将这些值以十六进制格式存储到文本文件中,使得矩阵中的每个值都由三位六进制值(12位)表示。即

十进制十六进制符号
150 255 25 096 0FF 019
400 80 10 - > 190 050 00A
240 68 190 0F0 044 0BE



我使用的是这样的

 `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,\\\
);
end`

结果为
十进制十六进制
150 255 25 96 FF 19
400 80 10 - > 190 50 0 A
240 68 190 F0 44 BE

在这方面帮助我。 。

解决方案

要插入前导零,您的 fprint 命令应该看起来像这样:

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


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..

解决方案

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

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

这篇关于如何使用matlab以特定的格式将值保存到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆