Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当对象在MATLAB中移动了一定距离时才打印对象_Matlab - Fatal编程技术网

仅当对象在MATLAB中移动了一定距离时才打印对象

仅当对象在MATLAB中移动了一定距离时才打印对象,matlab,Matlab,我有在3D散点图中绘制对象的代码。有些对象会移动,所以我有一个色标来显示哪些对象比其他对象移动得更远。是否有一种方法可以只显示移动了一定距离的对象,而忽略没有移动的对象(即它们没有显示在绘图上) 这是我写的代码: filename= 'testing.gif' FigHandle = figure('Position', [50, -30, 1000, 800]); for i=1:1001 s=1; scatter3(am.data(:,3,i),am.data(

我有在3D散点图中绘制对象的代码。有些对象会移动,所以我有一个色标来显示哪些对象比其他对象移动得更远。是否有一种方法可以只显示移动了一定距离的对象,而忽略没有移动的对象(即它们没有显示在绘图上)

这是我写的代码:

filename= 'testing.gif'
FigHandle = figure('Position', [50, -30, 1000, 800]);

for i=1:1001      
    s=1;
    scatter3(am.data(:,3,i),am.data(:,4,i),am.data(:,5,i),s,am.data(:,6,i))
    colorbar
    caxis([0,2.863])
    % axis([0 50 0 50 0 40]);
    T(i)=getframe
end

根据您的注释,您希望比较相对于初始位置的位移。这些已经封装在数据的第6列中。如果任何坐标未超过阈值,则将这些坐标标记为
NaN
,以便它们不会显示在绘图上。换句话说,给定阈值
thresh
(在您的评论中,这是
2.863
),执行以下操作:

filename= 'testing.gif'
FigHandle = figure('Position', [50, -30, 1000, 800]);
s = 1;

thresh = 2.863; %// Adjust if necessary

for i = 1:1001
    currentCoords = am.data(:,3:6,i); %// Get current co-ordinates
    %// Find distance between these co-ordinates and the initial ones
    %// that don't pass the threshold and set to NaN
    loc = currentCoords(:,4) <= thresh;
    currentCoords(loc,:) = NaN;

    %// Now plot the data
    scatter3(currentCoords(:,1), currentCoords(:,2) currentCoords(:,3), ...
             s, currentCoords(:,4));

    colorbar;
    caxis([0,2.863]);
    axis([0 50 0 50 0 50]); %// Restrict axes to ensure they don't change
                            %// when plotting each frame
    T(i)=getframe;
end
filename_video = 'out.mp4'; %// or 'out.avi';
codec_name = 'MPEG-4';
videoWriter = VideoWriter(filename_video, codec_name);
videoWriter.FrameRate = 20; %// Set frame rate here
videoWriter.Quality = 100; %// Set quality of each frame here
您需要阅读有关MATLAB支持哪些编解码器的文档,您可以将文件写入其中,但是
MPEG-4
非常流行,它适用于Windows 7系统和更高版本以及Mac OS X 10.7或更高版本。如果要保存在
AVI
容器中,当然可以这样做,但请确保为此指定了正确的编解码器。上述代码的前两行指定要保存的视频的输出文件名,以及要使用的编解码器。设置视频写入器后的下三行,并设置帧速率和每帧的质量(如果使用有损编解码器)。对于无损,质量设置不会起任何作用。有一大堆其他参数,你可以设置自定义你的视频将是什么样子,但我会留给你阅读的文件,以解决这个问题。要使基本视频正常工作,以上内容就足够了

接下来需要做的是打开文件进行写入,然后在
T
中循环浏览每个帧,并将帧写入此视频。完成后,关闭视频。换言之,接下来您将执行以下操作:

open(videoWriter); %// Open the video writer
for idx = 1 : numel(T)  %// Write the frames to file
    writeVideo(videoWriter, T(idx));
end
close(videoWriter); %// Close the video writer
如果一切正常,您将看到运行此代码的地方保存了一个
.mp4
(或
.avi
)文件,您应该能够使用任何常规视频查看软件(如)查看它。 我怎么强调都不过分,你需要打开文件进行写作,完成后,你可以关闭文件。在关闭文件之前,不会保存任何视频


祝你好运

我认为这还不对。当前的坐标-为什么是(:,3:6,i)?我不知道这是为什么。我们不需要减去位移。这已经是完全的流离失所。这有意义吗?还有,是。。。只是指下一行?我真的很感谢你的帮助!而且,当我运行它时,我只得到一帧——它应该生成一部电影。我只是得到一个包含所有物体的框架。@JacksonHart-我可能误解了你所说的总位移。这是否已经与初始位置有关?这意味着我甚至不需要跟踪第一个位置。在代码中,使用scatter3,独立访问第三列到第六列。我只是决定将其矢量化以使其更容易。是,
表示继续下一行。这是有效的MATLAB语法。正确,我们不需要初始位置。它隐含在位移柱中。其想法是制作一部只显示置换对象的电影。因此,随着时间的推移,每一帧都应该有越来越多的位移对象。@JacksonHart-太棒了。我会再编辑一次。我也不知道为什么你没有得到一个以上的帧。我会仔细检查我的密码。