回 帖 发 新 帖 刷新版面

主题:[讨论]matlab程序使用方法

% 用matlab产生hex文件
function hexfile(filename,var,databytenum,firstaddr,record_set,depth)
% function hexfile(filename,var,width,depth)
%       It creates a 'hex' file called filename,which be written with var.
%       The 'mif' file is a kind of file formats which is uesed in Altera's
%       EDA tool,like maxplus II ,quartus II,to initialize the memory
%       models,just like cam,rom,ram.
%       Using this function,you can easily produce the 'hex' file written
%       with  all kinds of your data.
%       If the size of 'var' is shorter than 'depth',0 will be written for the
%       lefts.If the size of 'var' is greater than 'depth',than only 'depth' former
%       data of 'var' will be written;
%       the radix of address and data is hex
%       filename --the name of the file to be created,eg,"a.hex",string;
%       var --the data to be writed to the file, can be 3D or less ,int or other fittable;
%       databytenum --the num of byte in the every record,int,dec;
%       firstaddr --the first address of the data to be stored in the memory,int,dec;
%       record_set --the record style of the data,int,dec;
%       depth --the number of the record to be writed,int;
%       because matlab read the matrix is colum first,if you want to write
%       the 'var' data in row first mode, just set var to var';
%       author:email --allstars52@gmail.com
%       Data:07/10/2007
%       example:
%            a=uint8(rand(16,1)*256);
%            hexfile('randnum.hex',a,2,0,0,16);
%       Intel hex 由一条或多条记录组成,每条记录都由一个冒号“:”打头,其格式如下:      
%       :CCAAAARR...ZZ
%   其中:
%       CC:本条记录中的数据字节数
%       AAAA :本条记录中的数据在存储区中的起始地址
%       RR :记录类型:
%    00 数据记录     (data record)
%    01 结束记录     (end record)
%    02 段记录       (paragraph record)
%    03 转移地址记录 (transfer address record)
%       ... :数据域
%       ZZ :数据域校验和
%       ZZ=01h+NOT(CC+AA+AA+RR+...)
fh=fopen(filename,'w+');
var=rem(var,depth+2);
[sx,sy,sz]=size(var);
value=var(1,1,1);
idepth=0;
temp=1;
temp1=uint8(0);
zz=0;
straddr=dec2hex(0,4);
strnum=dec2hex(0,databytenum);
strdata=dec2hex(0,databytenum);
strdatasum=0;
tatol=0;
for k=1:sz,
    for j=1:sy,
        for i=1:sx,
            if(~((i==1 ) &&( j==1) &&( k==1)))
               if(idepth<depth)
                  idepth=idepth+1;
                  firstaddr=firstaddr+1;
                  straddr=dec2hex(firstaddr-1,4);
                  for m=0:2(databytenum-1)*2)
                      strdata=dec2hex(value,2*databytenum);
                      x=1+m;
                      y=x+1;
                      strdatasum=strdatasum+hex2dec(strdata(x:y));
                  end
                   temp1=uint8(databytenum+hex2dec(straddr(1:2))+hex2dec(straddr(3:4))+record_set+strdatasum);
                   zz=dec2hex((1+bitxor(temp1, uint8(255))),2);%数据域校验和
                   tatol=[dec2hex(databytenum,2) dec2hex(firstaddr-1,4) dec2hex(record_set,2) dec2hex(value,2*databytenum) dec2hex((1+bitxor(temp1, uint8(255))),2)];
                   fprintf(fh,':%s\r\n',tatol);
                   strdatasum=0;
                else
                   fprintf(fh,':%s\r\n',tatol);
                   strdatasum=0;
               end
                   value=var(i,j,k);
            end
        end
    end
end
if(idepth<depth)
                 firstaddr=firstaddr+1;
                 straddr=dec2hex(firstaddr-1,4);
                           for m=0:2(databytenum-1)*2)
                               strdata=dec2hex(value,2*databytenum);
                               x=1+m;
                               y=x+1;
                               strdatasum=strdatasum+hex2dec(strdata(x:y));
                               end
                               temp1=uint8(databytenum+hex2dec(straddr(1:2))+hex2dec(straddr(3:4))+record_set+strdatasum);
                               zz=dec2hex((1+bitxor(temp1, uint8(255))),2);
                               tatol=[dec2hex(databytenum,2) dec2hex(firstaddr-1,4) dec2hex(record_set,2) dec2hex(value,2*databytenum) zz ];
                fprintf(fh,':%s\r\n',tatol);
              else
                fprintf(fh,':%s\r\n',tatol);
end
if(idepth<depth-1)
    if(idepth==(depth-2))
        fprintf(fh,':%s\r\n',0);
    else
        fprintf(fh,':%s\r\n',0);
    end
end
fprintf(fh,':00000001ff');   %hex文件结束标志           
fclose(fh);
别人帮我写的 matlab程序,我在command window 中直接复制后运行,出现下面的错误
但是别人说这个程序是正确的 ,希望大家指点下,是否要在 matlab中做何种设置呢 ?
??? function hexfile(filename,var,databytenum,firstaddr,record_set,depth)
    |
Error: Function definitions are not permitted at the prompt or in scripts.

回复列表 (共1个回复)

沙发

分别建立命令文件和函数文件,将华氏温度f转换为摄氏温度c。
程序1: 
首先建立命令文件并以文件名f2c.m存盘。
clear;            %清除工作空间中的变量
f=input('Input Fahrenheit temperature:');
c=5*(f-32)/9
然后在MATLAB的命令窗口中输入f2c,将会执行该命令文件,执行情况为:
Input Fahrenheit temperature:73
c =
   22.7778

我来回复

您尚未登录,请登录后再回复。点此登录或注册