Can this stacking code be simplified

1 visualización (últimos 30 días)
Martin
Martin el 27 de Ag. de 2019
Hello,
I need to create a figure (f) and insert a table. It works well:
f = figure('units','normalized');
pets = table({'22:00'},4,6,'VariableNames',{'Time','Dogs','Cats'})
uit = uitable(f);
uit.Data = {'Time','Dogs','Cats'; char(pets.Time(end)),num2str(pets.Dogs(end)),num2str(pets.Cats(end)) };
But in this case there are only 1 row in the pets table (which can happen in my code). The problem arrive when more rows exist in the table since I only need to show the last 7 rows. If there are fewer than 7 rows I need to show those.
Until now I got the following which works:
if size(pets,1) == 1
uit.Data = {'Time','Dogs','Cats'; char(pets.Time(end)),num2str(pets.Dogs(end)),num2str(pets.Cats(end)) };
elseif size(pets,1) == 2
uit.Data = {'Time','Dogs','Cats'; char(pets.Time(end-1)),num2str(pets.Dogs(end-1)),num2str(pets.Cats(end-1)) ;...
char(pets.Time(end)),num2str(pets.Dogs(end)),num2str(pets.Cats(end)) };
elseif size(pets,1) == 3
uit.Data = {'Time','Dogs','Cats'; char(pets.Time(end-2)),num2str(pets.Dogs(end-2)),num2str(pets.Cats(end-2)) ;...
char(pets.Time(end-1)),num2str(pets.Dogs(end-1)),num2str(pets.Cats(end-1)) ;...
char(pets.Time(end)),num2str(pets.Dogs(end)),num2str(pets.Cats(end)) };
elseif ..... % 7 times
I do that 7 times, but that is a lot of code! Anyone have a way around this?

Respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by