如何使用2D数组设置Matlab线图图的XData? [英] How can you set the XData of a Matlab line graph plot with a 2D array?

查看:263
本文介绍了如何使用2D数组设置Matlab线图图的XData?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在动态更改绘图上显示的数据时遇到了一些问题.数据集存储在尺寸可变的2D数组中,在程序运行期间,仅矩阵的宽度会更改-所有元素始终具有相同的长度.这里的意图是使矩阵的每一行在折线图上由一条线表示.

I'm having some issues dynamically changing the data that is displayed on a plot. The data set is stored in a 2D array of varying dimensions, during the running of the program, only the width of the matrix will change - all elements always have the same length. The intention here is to have each row of the matrix represented by a line on the line plot.

一个例子:

data = [1, 2, 3; 3, 4, 5; 4, 5, 6];
p = plot(data);
drawnow;

new_data = [7, 8, 9; 1, 2, 3; 4, 5, 6];
set(p, 'XData', new_data);
drawnow;

当数据格式更好时,此方法很好地用于更新图,但是我似乎找不到一种以有用的格式组织数据的方法,以允许对其进行绘制.

This method works well for updating plots when the data is better formatted, however I cannot seem to find a way to organise the data in a useful format that will allow this to plot.

推荐答案

要设置多个属性(由于p是线对象的句柄数组),属性名称以及要设置的属性必须是单元格数组,请参见例如此文档页面将线型设置为多行的不同值"下例如.

To set multiple properties (as p is an array of handles to line objects), the property name as well as the properties to set need to be cell arrays, see e.g. this documentation page under "Set Line Style to Different Value for Multiple Lines" for an example.

在您的示例情况下,这将起作用:

In your example case, this would work:

set(p, {'XData'}, mat2cell(new_data, ones(1,3), 3));

另请参见 mat2cell ,在这种情况下,我会使用它将new_data整形为3x1的单元格数组,每个数组包含来自new_data的1x3行.

See also mat2cell, in this case I use it to reshape new_data to a 3x1 cell array each containing a 1x3 row from new_data.

这篇关于如何使用2D数组设置Matlab线图图的XData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆