Problems with Tables which contain a single row.
显示 更早的评论
I have a GUI app working and just doing final testing. Everything is working very well..... except when I encounter a scenario where a table has a single row.
I have replicated the scenario with the following example...
Table3Rows has 3 rows and 3 columns. Table1Row is the same as Table3Rows but only contains the first row.
clc
var1 = ['abc';'def';'ghi'];
var2 = [1;2;3];
var3 = ['x';'y';'z'];
Table3Rows = table(var1,var2,var3) % This table has 3 rows
Table1Row = Table3Rows(1,:) % Same as table above but wth just the first row
TableVar1 =table(Table3Rows.var1) % This works perfectly (ie create a new table with just the first column)
TableVar1Row1 = table(Table1Row.var1) % This is identical except Table1Row has only 1 row but causes an error
The last 2 instructions are identical. The last instruction creates an error.
In a real world scenario, I cannot control how many rows may be in the table. It may be empty, 1, 10,1000's.
Any hints on how I can cater for the single row (within a table) scenario.
采纳的回答
更多回答(1 个)
Image Analyst
2022-8-28
Don't use table(). Try it using indexing:
var1 = ['abc';'def';'ghi'];
var2 = [1;2;3];
var3 = ['x';'y';'z'];
Table3Rows = table(var1,var2,var3) % This table has 3 rows
Table1Row = Table3Rows(1,:) % Same as table above but wth just the first row
TableVar1 = Table3Rows(:, 1) % Extract only the first column into a new table.
TableVar1Row1 = Table1Row(:, 1) % Extract only the first column into a new table.
类别
在 帮助中心 和 File Exchange 中查找有关 Testing Frameworks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!