I am trying to add rows at the end of table. For example:
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
Tab=table;
s=struct;
for i=1:5
s.name=LastName{i};
s.age=Age(i);
s.heigt=Height(i);
s.weight=Weight(i);
s.BP=BloodPressure(i);
temp=struct2table(s);
Tab(end+1,:)=temp;
end
The table is declared empty, it adds the 1st row, but in the second iteration of the for loop gives below error message:
Subscripted assignment dimension mismatch for table variable 'name'.
I understand that this happens because of the variable name has more characters in the second iteration. Is there any way to implement this?
This is a sample code I generated to explain my problem. In my actual code, the problem is similar, but a structure type variable is being returned from a different function which I can't modify.
First define the whole structure array:
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
s = struct('name',LastName,'age',num2cell(Age),...
'heigt',num2cell(Height),...
'weight',num2cell(Weight),...
'BP',num2cell(BloodPressure,2));
And then convert it to table:
Tab = struct2table(s);
The result:
Tab =
name age heigt weight BP
__________ ___ _____ ______ __________
'Smith' 38 71 176 124 93
'Johnson' 43 69 163 109 77
'Williams' 38 64 131 125 83
'Jones' 40 67 133 117 75
'Brown' 49 64 119 122 80
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.