A separate plot for each group of A,B and C

**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# Three columns (x,y,z) in table, Loop through table for each X plot Y vs Z

4 views (last 30 days)

Show older comments

table

X Y Z

A 1 3

A 2 4

A 3 10

B 1 4

B 2 4

B 4 6

C 0 1

C 1 4

C 2 5

C 3 7

C 4 8

in this scenario there will be three plots of Y vs Z. how do I structure the loop around

bar(table.Y,table.Z)

thank you.

### Accepted Answer

Star Strider
on 19 May 2022

Try this —

CA = {'A' 1 3

'A' 2 4

'A' 3 10

'B' 1 4

'B' 2 4

'B' 4 6

'C' 0 1

'C' 1 4

'C' 2 5

'C' 3 7

'C' 4 8};

T1 = cell2table(CA, 'VariableNames',{'X','Y','Z'})

T1 = 11×3 table

**X**

**Y**

**Z**

**_____**

**_**

**__**{'A'} 1 3 {'A'} 2 4 {'A'} 3 10 {'B'} 1 4 {'B'} 2 4 {'B'} 4 6 {'C'} 0 1 {'C'} 1 4 {'C'} 2 5 {'C'} 3 7 {'C'} 4 8

[U1,~,ix] = unique(T1.X);

L = numel(U1);

figure

for k = 1:L

subplot(L,1,k)

v = ix == k;

plot(T1.Y(v), T1.Z(v), '.-')

grid

xlabel('Y')

ylabel('Z')

title(U1{k})

ylim([0 10])

end

There are ways to do this that may be more efficient for large arrays, although they require more coding as well.

.

##### 24 Comments

Frederick Awuah-Gyasi
on 19 May 2022

Frederick Awuah-Gyasi
on 20 May 2022

Hi @Star Strider I'm running the code now with two unique columns now. so i did this instead . the new column is B

[U1,~,ix] = unique(T(:,1:2),'rows');

but i'm getting this error

Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To

select variables, use t(:,i) or for one variable t.(i). To select rows, use t(i,:).

Frederick Awuah-Gyasi
on 20 May 2022

in the meantime I will try concatenating the colums and use the old logic

Star Strider
on 20 May 2022

I may need to know what ‘T’ is (at least some representative rows of it).

The purpose of the unique call was to get numeric indices representing the first column. Conccatenating the first colum (with the alphanumeric indicators) with a numeric column will not work. That may be throwing the error, since I get the same error when I use your unique call with the existing table.

The findgroups function will work with ‘X’ and ‘Y’ to give a unique index (although here there are no duplicates in these data).

Other parts of the code change as well —

CA = {'A' 1 3

'A' 2 4

'A' 3 10

'B' 1 4

'B' 2 4

'B' 4 6

'C' 0 1

'C' 1 4

'C' 2 5

'C' 3 7

'C' 4 8};

T1 = cell2table(CA, 'VariableNames',{'X','Y','Z'})

T1 = 11×3 table

**X**

**Y**

**Z**

**_____**

**_**

**__**{'A'} 1 3 {'A'} 2 4 {'A'} 3 10 {'B'} 1 4 {'B'} 2 4 {'B'} 4 6 {'C'} 0 1 {'C'} 1 4 {'C'} 2 5 {'C'} 3 7 {'C'} 4 8

[ix,ID] = findgroups(T1(:,1:2))

ix = 11×1

1
2
3
4
5
6
7
8
9
10

ID = 11×2 table

**X**

**Y**

**_____**

**_**{'A'} 1 {'A'} 2 {'A'} 3 {'B'} 1 {'B'} 2 {'B'} 4 {'C'} 0 {'C'} 1 {'C'} 2 {'C'} 3 {'C'} 4

L = numel(ix);

figure

for k = 1:L

subplot(L,1,k)

v = ix == k;

plot(T1.Y(v), T1.Z(v), '.-')

grid

xlabel('Y')

ylabel('Z')

% title(U1{k})

ylim([0 10])

end

.

Frederick Awuah-Gyasi
on 21 May 2022

Thanks so much. So I concanated the 1st and 2nd column. This is a verison of the first code. this second version was to plot cumulations so another coulmn was added. that hurlde seems to have been crossed a last piece is to animate the plots. So for each A, B C I will have animated plots.

[U1,~,ix] = unique(T1.X);

L = numel(U1);

figure

for k = 1:L

subplot(L,1,k)

v = ix == k;

plot(T1.Y(v), T1.Z(v), '.-')

grid

xlabel('Y')

ylabel('Z')

title(U1{k})

ylim([0 10])

saveas(gcf,name)

animeVector(k) = getframe;

end

myWriter = VideoWriter('LevelMovie');

myWriter.FrameRate = 20;

open(myWriter);

writeVideo(myWriter,animeVector);

close(myWriter);

I modified the code as above but i'm getting this error :

Error using VideoWriter/writeVideo (line 414)

All 'cdata' fields in FRAMES must be the same size.

Really appecaite all the help.

Frederick Awuah-Gyasi
on 21 May 2022

Is there a comamand or parameter to pass so that 1, and 3 even though there are not data points still take the full space. like below. Not sure how both are not consistent.

Sorry to bother you with many questions. Thanks for the help.

Star Strider
on 21 May 2022

I have no recent experience with videoWriter, however I will do my best to see if I can get this to work.

I am eliminating the saveas call for the time being, since it may not work with the online Run feature here.

CA = {'A' 1 3

'A' 2 4

'A' 3 10

'B' 1 4

'B' 2 4

'B' 4 6

'C' 0 1

'C' 1 4

'C' 2 5

'C' 3 7

'C' 4 8};

T1 = cell2table(CA, 'VariableNames',{'X','Y','Z'})

T1 = 11×3 table

**X**

**Y**

**Z**

**_____**

**_**

**__**{'A'} 1 3 {'A'} 2 4 {'A'} 3 10 {'B'} 1 4 {'B'} 2 4 {'B'} 4 6 {'C'} 0 1 {'C'} 1 4 {'C'} 2 5 {'C'} 3 7 {'C'} 4 8

% [U1,~,ix] = unique(T1.X);

%

% L = numel(U1);

%

% figure

% for k = 1:L

% subplot(L,1,k)

% v = ix == k;

% plot(T1.Y(v), T1.Z(v), '.-')

% grid

% xlabel('Y')

% ylabel('Z')

% title(U1{k})

% ylim([0 10])

% end

[U1,~,ix] = unique(T1.X);

L = numel(U1);

figure

for k = 1:L

subplot(L,1,k)

v = ix == k;

plot(T1.Y(v), T1.Z(v), '.-')

grid

xlabel('Y')

ylabel('Z')

title(U1{k})

ylim([0 10])

% saveas(gcf,name)

GF = getframe(gcf) % Test

animeVector(k) = getframe(gcf);

end

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

myWriter = VideoWriter('LevelMovie');

myWriter.FrameRate = 20;

open(myWriter);

writeVideo(myWriter,animeVector);

close(myWriter);

Using:

animeVector(k) = getframe(gcf);

appears to run without error.

.

Star Strider
on 21 May 2022

My apologies, however at this point I am completely lost.

I have no idea what you want to do.

Frederick Awuah-Gyasi
on 21 May 2022

Frederick Awuah-Gyasi
on 21 May 2022

Edited: Frederick Awuah-Gyasi
on 21 May 2022

If my data is of this format with the added W column. How do I get the animation for A, B C. sepearately.

X W Y Z

A 1 0 3

A 1 2 3

A 1 3 3

A 2 0 5

A 2 2 7

A 2 3 8

B 1 0 3

B 1 1 3

B 1 3 3

B 2 0 12

B 2 2 1

B 2 3 3

C 1 0 2

C 1 1 1

C 1 2 3

C 2 0 4

C 2 1 3

C 2 2 6

I have edited the code to this to this :

[ix,ID] = findgroups(T1(:,1:2))

L = numel(U1);

figure

for k = 1:L

figure(k) %subplot(L,1,k)

v = ix == k;

plot(T1.Y(v), T1.Z(v), '.-')

grid

xlabel('Y')

ylabel('Z')

title(U1{k})

ylim([0 10])

GF = getframe(gcf) % Test

animeVector(k) = getframe(gcf);

end

myWriter = VideoWriter('LevelMovie');

myWriter.FrameRate = 20;

open(myWriter);

writeVideo(myWriter,animeVector);

close(myWriter);

Does it look right.

Star Strider
on 22 May 2022

I have no idea if it looks right, however with a few changes in the code, it runs without error —

CA = {'A' 1 0 3

'A' 1 2 3

'A' 1 3 3

'A' 2 0 5

'A' 2 2 7

'A' 2 3 8

'B' 1 0 3

'B' 1 1 3

'B' 1 3 3

'B' 2 0 12

'B' 2 2 1

'B' 2 3 3

'C' 1 0 2

'C' 1 1 1

'C' 1 2 3

'C' 2 0 4

'C' 2 1 3

'C' 2 2 6};

T1 = cell2table(CA,'VariableNames',{'X','W','Y','Z'})

T1 = 18×4 table

**X**

**W**

**Y**

**Z**

**_____**

**_**

**_**

**__**{'A'} 1 0 3 {'A'} 1 2 3 {'A'} 1 3 3 {'A'} 2 0 5 {'A'} 2 2 7 {'A'} 2 3 8 {'B'} 1 0 3 {'B'} 1 1 3 {'B'} 1 3 3 {'B'} 2 0 12 {'B'} 2 2 1 {'B'} 2 3 3 {'C'} 1 0 2 {'C'} 1 1 1 {'C'} 1 2 3 {'C'} 2 0 4

[ix,ID] = findgroups(T1(:,1:2))

ix = 18×1

1
1
1
2
2
2
3
3
3
4

ID = 6×2 table

**X**

**W**

**_____**

**_**{'A'} 1 {'A'} 2 {'B'} 1 {'B'} 2 {'C'} 1 {'C'} 2

L = size(ID,1);

figure

for k = 1:L

figure(k) %subplot(L,1,k)

v = ix == k;

plot(T1.Y(v), T1.Z(v), '.-')

grid

xlabel('Y')

ylabel('Z')

title(sprintf('%s %d',ID{k,1}{:},ID{k,2}))

ylim([0 10])

GF = getframe(gcf) % Test

animeVector(k) = getframe(gcf);

end

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

myWriter = VideoWriter('LevelMovie');

myWriter.FrameRate = 20;

open(myWriter);

writeVideo(myWriter,animeVector);

close(myWriter);

I have no idea how this relates to your other post Cross product of two tables without common key so I will leave that alone.

.

Frederick Awuah-Gyasi
on 22 May 2022

Thanks @Star Strider. Instead of A1 A2 B1 B2 C1 C2 for the animations. Will it be possible to have A, B C. So the animation shows A {A1 and A2 ) combined, B(B1 and B2) C (C1 and C2) . W is accually a day column shown ie. day 1 2 3 etc.

yeah that post is related but didnt want to confused the two. will explain that later. Z column must have data (0,1,2,3,4) there are instances where the are no data points so will have to fix that.

really appreciate all the help.

Star Strider
on 22 May 2022

I have no idea how you want to combine them.

Try this —

CA = {'A' 1 0 3

'A' 1 2 3

'A' 1 3 3

'A' 2 0 5

'A' 2 2 7

'A' 2 3 8

'B' 1 0 3

'B' 1 1 3

'B' 1 3 3

'B' 2 0 12

'B' 2 2 1

'B' 2 3 3

'C' 1 0 2

'C' 1 1 1

'C' 1 2 3

'C' 2 0 4

'C' 2 1 3

'C' 2 2 6};

T1 = cell2table(CA,'VariableNames',{'X','W','Y','Z'})

T1 = 18×4 table

**X**

**W**

**Y**

**Z**

**_____**

**_**

**_**

**__**{'A'} 1 0 3 {'A'} 1 2 3 {'A'} 1 3 3 {'A'} 2 0 5 {'A'} 2 2 7 {'A'} 2 3 8 {'B'} 1 0 3 {'B'} 1 1 3 {'B'} 1 3 3 {'B'} 2 0 12 {'B'} 2 2 1 {'B'} 2 3 3 {'C'} 1 0 2 {'C'} 1 1 1 {'C'} 1 2 3 {'C'} 2 0 4

[ix,ID] = findgroups(T1(:,1:2))

ix = 18×1

1
1
1
2
2
2
3
3
3
4

ID = 6×2 table

**X**

**W**

**_____**

**_**{'A'} 1 {'A'} 2 {'B'} 1 {'B'} 2 {'C'} 1 {'C'} 2

L = size(ID,1);

figure

for k = 1:2:L

figure(k) %subplot(L,1,k)

v = find(ix == k);

v2 = v:v+5;

plot(T1.Y(v2(1:3)), T1.Z(v2(1:3)), '.-')

hold on

plot(T1.Y(v2(1:3)+3), T1.Z(v2(1:3)+3), '.-')

hold off

grid

xlabel('Y')

ylabel('Z')

title(sprintf('%s',ID{k,1}{:}))

ylim([0 10])

GF = getframe(gcf) % Test

animeVector(k) = getframe(gcf);

end

v = 3×1

1
2
3

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

v = 3×1

7
8
9

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

v = 3×1

13
14
15

GF = struct with fields:

cdata: [433×577×3 uint8]
colormap: []

myWriter = VideoWriter('LevelMovie');

myWriter.FrameRate = 20;

open(myWriter);

writeVideo(myWriter,animeVector);

Error using VideoWriter/writeVideo

The 'cdata' field of FRAME must not be empty

The 'cdata' field of FRAME must not be empty

close(myWriter);

This combines the plots.

I defer to you to solve the writeVideo error.

.

Frederick Awuah-Gyasi
on 22 May 2022

Edited: Frederick Awuah-Gyasi
on 22 May 2022

@Star Strider: what I want to be able to do is loop through and have A1 and A2 producing one animation,B1,B2 producing a second animation and C1 C2 producing a 3rd animation.

this code

for k = 1:L

figure(k) %subplot(L,1,k)

v = find(ix == k);

plot(T1.Y(v), T1.Z(v), '.-')

grid

xlabel('Y')

ylabel('Z')

title(sprintf('%s %d',ID{k,1}{:},ID{k,2}))

ylim([0 10])

%GF = getframe(gcf) % Test

%Vector(k) = getframe(gcf);

end

produces A1, A2, B1, B2, C1, C2. I think my use of the word combine was wrong. wanted A1, A2 producing an animation separeate from B1, B2. Hope this makes sense. Sorry for bothering you so long.

Star Strider
on 23 May 2022

That is what my earlier code did.

Please experiment with the code I already wrote to get the result you want.

I am lost and obviously cannot figure this out.

Frederick Awuah-Gyasi
on 23 May 2022

Star Strider
on 23 May 2022

As always, my pleasure!

I just do not know where else to go with this, since we seem to be in an infinite loop.

Frederick Awuah-Gyasi
on 23 May 2022

@Star Strider this code produces 3 .mp4 files which I want. but Level_1_Movie_A.mp4 for instanfce should be just for A(show only A1 and A2) same for B and C. somehow the if condition is not working. Any ideas?

CA = {'A' 1 0 3

'A' 1 2 3

'A' 1 3 3

'A' 2 0 5

'A' 2 2 7

'A' 2 3 8

'B' 1 0 3

'B' 1 1 3

'B' 1 3 3

'B' 2 0 12

'B' 2 2 1

'B' 2 3 3

'C' 1 0 2

'C' 1 1 1

'C' 1 2 3

'C' 2 0 4

'C' 2 1 3

'C' 2 2 6};

T1 = cell2table(CA,'VariableNames',{'X','W','Y','Z'});

[ix,ID] = findgroups(T1(:,1:2));

[U1,~,ixx] = unique(T1.X);

L = size(ID,1);

Names = unique(T1.X)

M = numel(U1);

for b = 1:length(Names)

for k = 1:L

figure(k) %subplot(L,1,k)

%

v = find(ix == k);

data=unique(T1.X(v))

Names(b)

ismember( Names(b), data )

if ismember( Names(b), data )

T1.X(v)

plot(T1.Y(v), T1.Z(v), '.-')

grid

xlabel('Y')

ylabel('Z')

title(sprintf('%s %d',ID{k,1}{:},ID{k,2}))

ylim([0 10])

GF = getframe(gcf) % Test

Vector(k) = getframe(gcf);

end

end

moviename = strcat('Level_1_Movie_', Names(b))

myWriter = VideoWriter(moviename{1},'MPEG-4');

myWriter.FrameRate = 20;

open(myWriter);

writeVideo(myWriter,Vector);

close(myWriter);

end

Frederick Awuah-Gyasi
on 23 May 2022

Star Strider
on 23 May 2022

As always, my pleasure!

Perhaps someone else has some new insights that I do have not at this point.

### More Answers (1)

KSSV
on 19 May 2022

Let data be your three column matrix.

x = data(:,1) ;

y = data(:,2) ;

z = data(:,3) ;

xi = unique(x) ;

yi = unique(y) ;

nx = length(xi) ;

ny = length(yi) ;

% Matrices

[X,Y] = meshgrid(x,y) ;

Z = reshape(z,[ny,nx]) ;

##### 1 Comment

Frederick Awuah-Gyasi
on 19 May 2022

Thank you. I will try this out. But was hoping for example

For X in list (X) :

plot(Y,Z)

For if X = A

the data to plot will beb

Y Z

1 3

2 4

3 10

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)