【问题标题】:How to do an animate plot in MATLAB from a sequence of matrices如何在 MATLAB 中从矩阵序列中绘制动画图
【发布时间】:2021-07-25 11:24:20
【问题描述】:

我有使用 Wolff 算法在 MATLAB 中模拟 XY 模型的代码,我想实现一个 pcolor/color map 来根据它们在系统中的角度来演示每个旋转。但我希望它随着角度的变化而变化。

知道怎么做吗?

这是我希望它看起来如何的示例https://i.stack.imgur.com/aSp7s.png

【问题讨论】:

    标签: matlab visualization montecarlo colormap


    【解决方案1】:

    如果将晶格的每个快照保存在元胞数组 A{t} 中,则可以使用以下函数查看并保存为视频(如果 fileName 不为空,则该函数保存一个 mp4 视频)。

    另一种选择是调整函数view_lattice 来运行您的模拟(老实说,出于性能问题,我不建议这样做)。我会标记你应该在哪里编辑以进行“实时”模拟

    这至少是 MATLAB R2019b(虽然它可能与早期版本兼容,但不能保证)。

    文件view_lattice.m

    function view_lattice(A,fileName)
    % for a 'live' simulation, you will have to remove A from the input 
    % parameters and add the ones you need for the XY Wolff algorithm,
    % which will be used to calculate each configuration A in the time loop below
    % you will also need to remove the assert statements for 'live' simulation
    %
    % otherwise, you save snapshots from your simulation
    % and use this function as is
    % 
    % A -> A{k}[m,n] snapshot k containing the angles of spins in lattice site at row m and col n
    % fileName -> if contains string, then records a video with the snapshots and name it with this string
        assert(iscell(A) && all(cellfun(@(a)isnumeric(a) && ismatrix(a),A)),'A must be cell of numeric matrices');
        assert(ischar(fileName),'fileName must be either an empty char or contain a file name');
        
        recordVideo = ~isempty(fileName);
        
        if recordVideo
            vw = setup_video(fileName);
        else
            vw = [];
        end
        % setting some default axis properties to speed-up plotting
        set(0,'DefaultAxesPlotBoxAspectRatio',[1 1 1],'DefaultAxesDataAspectRatioMode','manual','DefaultAxesDataAspectRatio',[1,1,1],'DefaultAxesNextPlot','replace');
        
        fh = figure;
        ax=axes;
        for t = 1:numel(A) % for 'live' simulation, this loop should be the time loop
            % here you calculate the new configuration A
            % and call the function below with A instead of A{t}
            vw = record_frame(vw,fh,ax,A{t},t,recordVideo);
        end
        
        % any video to close?
        if recordVideo
            vw.close();
        end
    end
    
    function vw = record_frame(vw,fh,ax,A,t,recordVideo)
        imagesc(ax,A);
        title(ax,sprintf('snapshot %g',t)); % if you want, y
        axis(ax,'square');
        daspect(ax,[1,1,1]);
        pause(0.01);
        if recordVideo
            vframe = getframe(fh);
            vw.writeVideo(vframe);
        end
    end
    
    function vw = setup_video(fileName)
        vid_id = num2str(rand,'%.16g');
        vid_id = vid_id(3:6);
        vid_id = [fileName,'_',vid_id];
        % Initialize video
        vw = VideoWriter([vid_id,'.mp4'], 'MPEG-4'); %open video file
        vw.Quality = 100;
        vw.FrameRate = 16;
        vw.open();
    end
    

    测试脚本:test.m

    clearvars
    close all
    
    A = cell(1,30);
    
    for t = 1:numel(A)
        % creating a sequence of random snapshots only for illustration
        A{t} = rand(20,20);
    end
    
    % viewing the animation and saving it as a video with name test
    view_lattice(A,'test');
    

    输出

    【讨论】:

    • 感谢您的帮助,我不太擅长 MATLAB,而且我基本上是在调整代码,所以我需要回复您这是否适合我的需要。
    猜你喜欢
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多