I am having trouble understanding this error "To assign to or create a variable in a table, the number of rows must match the height of the table.", can someone explain?

46 次查看(过去 30 天)
As stated in the title, I'm struggling with getting rid of error message "To assign to or create a variable in a table, the number of rows must match the height of the table."
Here is a snippit of my original code. The line specifically causing hte probled is the line within the For loop. Sorry if this is a simple question, I do not usually work MATLAB and got thrown into this project. Thank you!
units_tbl(1,:)
values_tbl_new = vertcat(units_tbl(1,:),values_tbl);
for k = 1:length(vars_wanted)
values_tbl_new.(vars_wanted(k)) = values_tbl.(vars_wanted(k));
end
  1 个评论
Stephen23
Stephen23 2024-7-31
编辑:Stephen23 2024-7-31
"I am having trouble understanding this error "To assign to or create a variable in a table, the number of rows must match the height of the table.", can someone explain?"
Here is a table with three rows:
T = array2table(randi(9,3,4))
T = 3x4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 9 2 5 6 4 1 8 6 5 4 8 7
Creating a new column/variable which also has three rows will work correctly:
T.('new') = [1;99;Inf]
T = 3x5 table
Var1 Var2 Var3 Var4 new ____ ____ ____ ____ ___ 9 2 5 6 1 4 1 8 6 99 5 4 8 7 Inf
Creating a new column/variable which does not have three rows will fail:
T.('bad') = [1;2;3;4;5;6]
Error using . (line 507)
To assign to or create a variable in a table, the number of rows must match the height of the table.

请先登录,再进行评论。

回答(1 个)

Cris LaPierre
Cris LaPierre 2024-7-31
Values_tbl_new has n+1 rows because you vertically concatenate the first row of units_tbl to values_tbl.
You then try to assign one column (or variable) of table values_tbl to a colum (or variable) of values_tbl_new. The number of rows differ between these two tables by 1, hence the error.
I would suggest not placing your units in the first row. Instead, add it to the table properties. The VariableUnits one would make sense to me.
That might look like this.
values_tbl_new = table();
values_tbl_nes.Properties.VariableUnits = units_tbl(1,:);
for k = 1:length(vars_wanted)
values_tbl_new.(vars_wanted(k)) = values_tbl.(vars_wanted(k));
end
  2 个评论
Cris LaPierre
Cris LaPierre 2024-7-31
Here's an example of how I would do it.
values_tbl = readtable('patients.xls')
values_tbl = 100x10 table
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus ____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________ {'Smith' } {'Male' } 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'} {'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' } {'Williams'} {'Female'} 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' } {'Jones' } {'Female'} 40 {'VA Hospital' } 67 133 false 117 75 {'Fair' } {'Brown' } {'Female'} 49 {'County General Hospital' } 64 119 false 122 80 {'Good' } {'Davis' } {'Female'} 46 {'St. Mary's Medical Center'} 68 142 false 121 70 {'Good' } {'Miller' } {'Female'} 33 {'VA Hospital' } 64 142 true 130 88 {'Good' } {'Wilson' } {'Male' } 40 {'VA Hospital' } 68 180 false 115 82 {'Good' } {'Moore' } {'Male' } 28 {'St. Mary's Medical Center'} 68 183 false 115 78 {'Excellent'} {'Taylor' } {'Female'} 31 {'County General Hospital' } 66 132 false 118 86 {'Excellent'} {'Anderson'} {'Female'} 45 {'County General Hospital' } 68 128 false 114 77 {'Excellent'} {'Thomas' } {'Female'} 42 {'St. Mary's Medical Center'} 66 137 false 115 68 {'Poor' } {'Jackson' } {'Male' } 25 {'VA Hospital' } 71 174 false 127 74 {'Poor' } {'White' } {'Male' } 39 {'VA Hospital' } 72 202 true 130 95 {'Excellent'} {'Harris' } {'Female'} 36 {'St. Mary's Medical Center'} 65 129 false 114 79 {'Good' } {'Martin' } {'Male' } 48 {'VA Hospital' } 71 181 true 130 92 {'Good' }
vars_wanted = ["Height" "Weight" "Smoker"];
values_tbl_new = values_tbl(:,ismember(values_tbl.Properties.VariableNames,vars_wanted))
values_tbl_new = 100x3 table
Height Weight Smoker ______ ______ ______ 71 176 true 69 163 false 64 131 false 67 133 false 64 119 false 68 142 false 64 142 true 68 180 false 68 183 false 66 132 false 68 128 false 66 137 false 71 174 false 72 202 true 65 129 false 71 181 true
Steven Lord
Steven Lord 2024-7-31
Or even simpler, skip the ismember call and the direct request for the variable names from the table property. Just use indexing.
values_tbl = readtable('patients.xls');
head(values_tbl) % Show just the first few rows
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus ____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________ {'Smith' } {'Male' } 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'} {'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' } {'Williams'} {'Female'} 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' } {'Jones' } {'Female'} 40 {'VA Hospital' } 67 133 false 117 75 {'Fair' } {'Brown' } {'Female'} 49 {'County General Hospital' } 64 119 false 122 80 {'Good' } {'Davis' } {'Female'} 46 {'St. Mary's Medical Center'} 68 142 false 121 70 {'Good' } {'Miller' } {'Female'} 33 {'VA Hospital' } 64 142 true 130 88 {'Good' } {'Wilson' } {'Male' } 40 {'VA Hospital' } 68 180 false 115 82 {'Good' }
vars_wanted = ["Height" "Weight" "Smoker"];
values_tbl_new = values_tbl(:, vars_wanted); % Indexing using variable names
head(values_tbl_new) % Show just the first few rows
Height Weight Smoker ______ ______ ______ 71 176 true 69 163 false 64 131 false 67 133 false 64 119 false 68 142 false 64 142 true 68 180 false
See the "Index by Variable Names" section on this documentation page. The table at the end of that documentation page describes other techniques / syntaxes to access data in a table.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by