Problems with Tables which contain a single row.
Show older comments
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.
Accepted Answer
More Answers (1)
Image Analyst
on 28 Aug 2022
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.
1 Comment
Matt O'Brien
on 28 Aug 2022
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!