美文网首页
2018-11-09 matlab数据可视化并保存为gif

2018-11-09 matlab数据可视化并保存为gif

作者: gracebeibei | 来源:发表于2018-11-09 22:38 被阅读0次
    clear;close all;clc
    [~,~,raw]=xlsread('C:\Users\Administrator\Desktop\grace_study_record\数据可视化\matlab_动态图\example.csv');
    names={'张三','李四','王二麻子'};
    colors={[0,0,1],[0,1,0],[1,0,0]};
    width=1;          %每个条形的宽度
    
    filename='test02.gif';
    data_count=(size(raw,1)-1)/3;  %%%一共多少天的数据
    Fig=figure;
    
    for index=1:data_count
        AA=cell2mat(raw((index-1)*3+1+1,3));
        BB=cell2mat(raw((index-1)*3+2+1,3));
        CC=cell2mat(raw((index-1)*3+3+1,3));
        data=[AA BB CC];
        [data_sort,index_sort]=sort(data);    
        data_display=diag(data_sort);
        colors_display={colors{index_sort(1)},colors{index_sort(2)},colors{index_sort(3)}};
        names_display={names{index_sort(1)},names{index_sort(2)},names{index_sort(3)}};
         
        b=barh(data_display,width);  
        for i=1:length(data)
            b(i).FaceColor=cell2mat(colors_display(i));
        end
        set(gca,'yticklabel',names_display);
        
        text(max(data)*0.9,1,raw((index-1)*3+1+1,4),'color','r','FontSize',14)
        drawnow
        
        frame=getframe(Fig);
        im=frame2im(frame);
        [imind,cm]=rgb2ind(im,256);
        if index==1
            imwrite(imind,cm,filename,'gif','WriteMode','overwrite','Loopcount',inf);
        else
            imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',0.02);
        end
    end
    
    test02.gif

    相关文章

      网友评论

          本文标题:2018-11-09 matlab数据可视化并保存为gif

          本文链接:https://www.haomeiwen.com/subject/bpvoxqtx.html