StackedPlot with two tables with specific variables

I have two tables say table A and table B. A and B has different number of variables but the same number of rows.
following the syntax of stackedplot if I want to do a stackedplot for A
I would write something like this stackedplot(A,[''variable name 1''," [variable name 2']")
My question:
I want to have stacked plot of two tables A and B but specific variables from table A and specific variables from table B
stackedplot(A,["variable1ofA","var2ofA", "var3ofA"],B,["var1ofB","var2ofB"])
The syntax for multiple table is stackedplot(tbl1,tabl2) how do I specify which variables of each table ?
How can I achieve this ?
Thank you

 Réponse acceptée

If they have the same number of rows, I think what you would want to do is
T = [A(:,["variable1ofA","var2ofA", "var3ofA"]),B(:,["var1ofB","var2ofB"])]
stackedplot(T)
If the tables do not share the same variable names, this may work as well (untested)
stackedplot([A,B],["variable1ofA","var2ofA", "var3ofA","var1ofB","var2ofB"])

6 commentaires

Bhargav
Bhargav le 9 Fév 2024
Modifié(e) : Bhargav le 9 Fév 2024
Thank you so much !
This worked like a charm, Follow up question: Can I extend this logic to multiple tables ?
also what if they do not have same number of rows ?
also how do I pinpoint max and minimum value in a for a particular variable in stacked plot
If my stacked plot contains 5 graphs, I just want to high light max and min for say variable 3 in the stacked plot
The first syntax should work for as many tables as stackedplot will allow, regardless of the number of rows in each table.
The 2nd syntax requires both tables have the same number of rows.
About adding max and min, that doesn't appear to be something that is going to be easy to do with a stackedplot. I would look into using tiledlayout if that is what you want to do.
For displaying min and max, if you do want to use stackedplots with multiple tables, then you will need to create a table variable that is an array, meaning one column contains the line data, the other contains the max/min data. Here's a simple example borrowing from @Star Strider's example below.
Note that if you do not specify 'XVariable', then index number is used
% Create 2 tables with a different number fo rows
x1 = (0:0.1:25).';
x2 = (0:0.2:25).';
TA = array2table([x1 sin((1:4).*2.*pi.*x1/25)]);
TB = array2table([x2 sin((5:8).*2.*pi.*x2/25)]);
% identify max/min
Var6 = nan(height(TA),1);
[mn,idn] = min(TA.Var4);
[mx,idx] = max(TA.Var4);
Var6(idn) = mn;
Var6(idx) = mx;
% Create new table variable with both the line data and the min/max points.
TA.Var6 = [TA.Var4,Var6]
TA = 251×6 table
Var1 Var2 Var3 Var4 Var5 Var6 ____ ________ ________ ________ _______ ____________________ 0 0 0 0 0 0 NaN 0.1 0.02513 0.050244 0.075327 0.10036 0.075327 NaN 0.2 0.050244 0.10036 0.15023 0.19971 0.15023 NaN 0.3 0.075327 0.15023 0.22427 0.29704 0.22427 NaN 0.4 0.10036 0.19971 0.29704 0.39137 0.29704 NaN 0.5 0.12533 0.24869 0.36812 0.48175 0.36812 NaN 0.6 0.15023 0.29704 0.43712 0.56727 0.43712 NaN 0.7 0.17502 0.34464 0.50362 0.64706 0.50362 NaN 0.8 0.19971 0.39137 0.56727 0.72031 0.56727 NaN 0.9 0.22427 0.43712 0.62769 0.78629 0.62769 NaN 1 0.24869 0.48175 0.68455 0.84433 0.68455 NaN 1.1 0.27295 0.52517 0.73751 0.89384 0.73751 NaN 1.2 0.29704 0.56727 0.78629 0.93433 0.78629 NaN 1.3 0.32094 0.60793 0.8306 0.96538 0.8306 NaN 1.4 0.34464 0.64706 0.87018 0.98669 0.87018 NaN 1.5 0.36812 0.68455 0.90483 0.99803 0.90483 NaN
s = stackedplot(TB(:,[1 2 3]), TA(:,[1 3 6]),"CombineMatchingNames",false,"XVariable",'Var1');
sLineProperties(4).PlotType = {'plot','scatter'};
s.LineProperties(4).LineStyle={'-','none'};
s.LineProperties(4).Marker={'none','o'};
s.LineProperties(4).MarkerFaceColor=[0 1 1];
s.LineProperties(4).MarkerEdgeColor=[0 1 1];
I will try to implement the min and max method and see the results
Thank you so much for the help.
PS the second code snippet for plotting 2 tables with specific variables (which was untested) did not work
The 2nd syntax should work. I did have to make sure there are no shared variable names between the 2 tables, and that both tables have the same number of rows. Here's a working example.
x = (0:0.1:25).';
TA = array2table([x sin((1:4).*2.*pi.*x/25)]);
TB = array2table([x sin((5:8).*2.*pi.*x/25)]);
TB.Properties.VariableNames = ["X","Y1","Y2","Y3","Y4"];
stackedplot([TA,TB],["Var2","Var3", "Var4","Y1","Y2"])
ah I see. I think I have one shared variable hence it was giving me a duplicated error

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by