%% Combine Table and Vector Data
% Many plots that support tables allow you to specify some aspects of your plot using a table variable and other aspects using vectors or matrices. For example, you can create a scatter plot using coordinates from a table and customize the colors of the markers by setting the CData property to a vector, an RGB triplet, or a matrix of RGB triplets.
% Create a scatter plot using data from a table. Read patients.xls as a table, and plot the Weight versus the Height variables.

tbl = readtable("patients.xls");
figure
s = scatter(tbl,"Height","Weight","filled");

%
% Next, change the colors of the plotted points using a vector. When you combine data from different sources like this, the size of each vector, matrix, or table variable must be compatible with the plot you are creating. In this case, create a vector called bpratio by dividing the systolic values by the diastolic values from the table. Because bpratio is derived from the same table as the Height and Weight variables, it has the same number of elements as those variables, and so it is compatible with this plot.
% Color each point according to the blood pressure ratio by setting the CData property to bpratio. Then add a colorbar.

% Vary the color by blood pressure ratio
bpratio = tbl.Systolic./tbl.Diastolic;
s.CData = bpratio;

% Add a colorbar
colorbar

%
% You can also plot vectors or matrices, and modify the plot using table variables. After you create the plot, set the SourceTable property, and then set the table-related properties that you want. Table-related properties typically have the word Variable in their names. For example, plot two vectors of 100 random numbers.

x = rand(100,1);
y = rand(100,1);
figure
s = scatter(x,y,"filled");

%
% Change the marker colors so that they vary according to the values in a table variable. Read patients.xls as the table tbl. Set the SourceTable property and vary the marker colors according to the Age variable in the table. Because the table has 100 rows, and the plot has 100 points, the Age variable is compatible with the plot. Then, add a colorbar to the plot.

% Set source table and vary color by age
s.SourceTable = tbl;
s.ColorVariable = "Age";

% Add a colorbar
colorbar

%
% Note: Standalone visualizations such as heatmap do not support combinations of table and vector data.
% Copyright 2020 The MathWorks, Inc.