Matlab:如何在适合的特定行号中添加数据

时间:2016-06-11 17:43:20

标签: matlab matlab-uitable

问题1

我写了一个gui代码,它给出了以下一行,然后显示在一个可用的

combt =

Columns 1 through 5

2000    2530.4    2671.4    2.3   2.6

此行将自动添加到uitable中的第1行:

See here http://imageshack.com/a/img924/4946/I5s6NH.png 在上面的例子中,我希望将这一行添加到第2000行(取决于我的数据的第1列)。

因此,如果我的行有column_1 = 10,我希望此行显示在uitable的第10行,依此类推。我一直在寻找这个答案超过一年,所以你的帮助将不胜感激。

问题2:

说我有一张包含以下数字的表

combt =

Columns 1 through 5

1    2630.4    2671.4    5.3   2.6
2    2530.2    2673.4    2.2   6.6
10   2331.4    4671.2    4.3   2.7
11   2550.4    6671.4    2.1   2.8

如何重新排列它们,以便第1列对应于行号,空格用零填充:

combt =

Columns 1 through 5

1    2630.4    2671.4    5.3   2.6
2    2530.2    2673.4    2.2   6.6
0    0         0         0     0       (This is row 3)
0    0         0         0     0       (This is row 4)
0    0         0         0     0       (This is row 5)
0    0         0         0     0       (This is row 6)
0    0         0         0     0       (This is row 7)
0    0         0         0     0       (This is row 8)
0    0         0         0     0       (This is row 9)
10   2331.4    4671.2    4.3   2.7
11   2550.4    6671.4    2.1   2.8

1 个答案:

答案 0 :(得分:1)

使用new_d(d(:,1),:)=d创建新的必需矩阵new_d。这是一个示例代码。

d = [1    2630.4    2671.4    5.3   2.6; ...
2    2530.2    2673.4    2.2   6.6; ...
10   2331.4    4671.2    4.3   2.7; ...
11   2550.4    6671.4    2.1   2.8];

new_d(d(:,1),:)=d;

% with first column
f = figure();
t = uitable(f,'Data',new_d);

% without showing first column
f1 = figure();
t1 = uitable(f1,'Data',new_d(:,2:end));

使用新数据更新uitable

%% update table
% new data containing same first column value 10 and 2 
d1 = [14    2630.4    2671.4    5.3   2.6; ...   
18    2530.2    2673.4    2.2   6.6; ...
10   3333.4    2671.2    3.3   1.7; ...
2   2331.4    4671.2    4.3   2.7];

% update data 
new_d(d1(:,1),:)=d1;

% update uitable with first column
set(t,'Data',new_d);

% update uitable without showing first column
set(t1,'Data',new_d(:,2:end));