文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>matlab文件及字符处理例子

matlab文件及字符处理例子

时间:2009-06-24  来源:redoc

本例子读取一个文本文件,并将其转成matlab中的矩阵形式。文本文件的格式如下:

P num,px1,py1,px2,py2,...,px5,py5,

 ...

pxn,pyn;

其中P表示多边形,num是该多边形的顶点,pxi和pyi,i=1,...,n是多边形每一个顶点的坐标。如果左边位置不是数字而是'*',则表明该坐标与前一个顶点相应的坐标数值相同。如3,4,*,5表示的顶点(3,4)和顶点(3,5)。文件每行5个点的坐标,最后以分号结尾。

 

function mask=readalp(alp_file, grid_size)
    [fid, message] = fopen(alp_file);
    if(-1 == fid)
        input(message);
        exit;
    end
    polynum = 1;
    
    while ~feof(fid)
        line = fgetl(fid);
        llength = size(line);
        x=[];y=[];
        if line(1,1) == 'P'
            x=[]; y=[];
            if line(1, llength(1,2)) == ';'
                leave = 1;
                line(1, llength(1,2)) = ',';
            else
                leave = 0;
            end
            line = strrep(line, ',', ' ');
            linecell = strsplit(line, ' ');
            pointnum=str2num(linecell{2});
            [tmp index]=size(linecell);
            for i=3:2:index
                if linecell{i} == '*'
                    tmpx=size(x);
                    x =[x x(1, tmpx(1,2))];
                else
                    x=[x str2num(linecell{i})];
                end
                if linecell{i+1} == '*'
                    tmpy=size(y);
                    y= [y y(1, tmpy(1,2))];
                else
                    y=[y str2num(linecell{i+1})];
                end
            end
            while 1
                if leave == 1
                    break;
                end
                line='';
                line=fgetl(fid);
                llength=size(line);
                if line(1, llength(1,2)) == ';'
                    leave = 1;
                    line(1, llength(1,2)) = ',';
                else
                    leave = 0;
                end
                line=strrep(line, ',', ' ');
                linecell=strsplit(line, ' ');
                [tmp index]=size(linecell);
                for i =1:2:index
                    if linecell{i} == '*'
                        tmpx=size(x);
                        x=[x x(1, tmpx(1,2))];
                    else
                        x=[x str2num(linecell{i})];
                    end
                    if linecell{i+1} == '*'
                        tmpy=size(y);
                        y=[y y(1, tmpy(1,2))];
                    else
                        y=[y str2num(linecell{i+1})];
                    end
                end
            end
            polyx{polynum}=x;
            polyy{polynum}=y;
            polynum = polynum +1;
        end
    end
    fclose(fid);
    minx=min(polyx{1});
    miny=min(polyy{1});
    maxx=max(polyx{1});
    maxy=max(polyy{1});
    for i = 2:polynum-1
        if minx > min(polyx{i})
            minx = min(polyx{i});
        end
        if maxx < max(polyx{i})
            maxx = max(polyx{i});
        end
        if miny > min(polyy{i})
            miny = min(polyy{i});
        end
        if maxy < max(polyy{i})
            maxy = max(polyy{i});
        end
    end
    for i = 1:polynum-1
        polyx{i}=ceil((polyx{i}-minx)./grid_size);
        polyy{i}=ceil((polyy{i}-miny)./grid_size);
    end
    M=ceil((maxx-minx)/grid_size);
    N=ceil((maxy-miny)/grid_size);
    pm = zeros(N,M);
    for i=1:polynum-1
        pm =pm+poly2mask(polyx{i}, polyy{i}, N, M);
    end
    mask = pm;
    size(mask)
    imshow(mask)
end

参数grid_size是将多边形转化为矩阵型式中每一个格点的大小。

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载