Search code examples
matlabmatlab-uitable

Matlab: How to add data to specific row number in a uitable


Question 1

I wrote a gui code that gives the following row which is then made to display on a uitable:

combt =

Columns 1 through 5

2000    2530.4    2671.4    2.3   2.6

This row will be automatically added to row 1 in the uitable:

See here In my example above, I would like this row to be added to row 2000 (depending on column 1 of my data).

Therefore if my row has a column_1 = 10, I would like this row to appear at row 10 in the uitable and so on. I have been looking for this answer for over a year so your help will be much appreciated.

Question 2:

Say I have a table with the following numbers

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

How can rearrange them so that ,again, column 1 corresponds to the row number and the spaces are filled with zeros as such:

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

Solution

  • Use new_d(d(:,1),:)=d to create your new required matrix new_d. Here is a sample code.

    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));
    

    Update uitable with new data :

    %% 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));