set count numbers of rows in a uitable

Hi,
I'm trying to modify the first column number for each row( 1 2 3 ...) in an uitable based of each entry from another uitable:
D=get(handles.uitable1,'Data');%GETTING DATA FROM FIRST TABLE
Index=get(handles.uitable1,'UserData');
a = D(Index.Indices(:,1), :);
old_data=get(handles.uitable4,'Data'); %GETTING OLD DATA FROM SECOND TABLE
data_azi = datetime;
data_azi.Format = 'MM.dd.yyyy';
a = array2table(a);
a.a1 = 1;
a.a4 = a.a2;
a.a2 = char(data_azi);
a.a2 = {a.a2};
a = table2cell(a);
new_data = [old_data; a];
new_data = array2table(new_data);
numarnou = height(new_data);% GET HEIGHT OF OLD AND NEW TABLE
new_data.new_data2 = numarnou;% MODIFY THE RESULT OF ROWS HEIGHTS IN FIRST COLUMN
new_data.new_data2 = {new_data.new_data2};
new_data = table2cell(new_data);
set(handles.uitable4, 'Data',new_data); %SETTING DATA FROM FIRST TABLE
The error:
To assign to or create a variable in a table, the number of rows must match the height of the table.
Where am I wrong?

6 Comments

@Cristian Martin - please copy and paste the full error message so that we can get an idea of which line is generating the error.
Error using Generator_v12>pushbutton5_Callback (line 291)
To assign to or create a variable in a table, the number of rows must match the height of the table.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Generator_v12 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Generator_v12('pushbutton5_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Seems you added an additional row with variable a under the existing uitable4.Data (variable old_data) to create a new variable new_data. Hence the number of rows (new_data) does not match with the current table (uitable4) height.
@Simon Chan every time I execute pushbutton i get a new line of data from another table . if i just execute disp(new_data.new_data2); it's working.
new_data = [old_data; a];
You've catenated more data onto the existing from uitable1 so the height is the total of both tables.
The uitable object is not dynamically resized; I've not used it extensively, but I think the only way to add data entails recreating a new table. Seems klunky, but I've not seen anything that implies there is an addrow functionality.

Sign in to comment.

 Accepted Answer

I have found the solution:
old_data=get(handles.uitable4,'Data');
old_data = array2table(old_data);
nr_crt = height(old_data); %HERE I GET THE HEIGHT OF OLD DATA
old_data = table2cell(old_data);
data_azi = datetime;
data_azi.Format = 'MM.dd.yyyy';
a = array2table(a);
a.a1 = 1 + nr_crt; %HERE I ADD THE HEIGHT OF OLD DATA TO THE FIRST COLUMN OF THE NEXT ROW IN SECOND TABLE
a.a4 = a.a2;
a.a2 = char(data_azi);
a.a2 = {a.a2};
a = table2cell(a);
new_data = [old_data; a];
set(handles.uitable4, 'Data',new_data);

2 Comments

It;s weird to accept my answer , no?
It's OK. I now recall I had seen that before but I just couldn't recall it before...I hadn't used the uitable enough to recall it, but I shoulda'.

Sign in to comment.

More Answers (0)

Products

Release

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!