How do you plot two graphs with multiple curves next to a table?
조회 수: 13 (최근 30일)
이전 댓글 표시
I have essentially a multi-dimensional table. Because I don't know a better way, I have chosen to put it all in one table with blanks to portray the idea of another dimension (see image of table below). My goal is to show this table next to two graphs that have a lot of the same information contained. I have been able to get those two pieces separately, but can't find how to put them all in one screen.
The input data is subject to change, including the number of speed elements, number of deployment elements, etc.
I have tried wrapping my head around the difference between "tiledlayout", "subplots", and "uifigure/uitable/etc." and attempted this multiple different ways with no success.
Here is my current example code. Not the exact code I am working with but very similar:
z
% Input data
Speeds = [40,80,120];
ExampleData = strings(9,5);
ExampleData(:,3:end) = rand(9,3);
for n = 1:length(Speeds)
ExampleData(n+(2*(n-1)),1) = Speeds(n);
ExampleData(n+(2*(n-1)):n+(2*(n-1))+2,2) = ([5;10;15]);
end
% Create table
Headings = ["Speed" "Deployment" "Drag" "Lift" "Strain"];
DataTable = array2table(ExampleData,'VariableNames',Headings);
fig = uifigure;
uit = uitable(fig,"Data",DataTable);
% Color missing elements red
styleIndices = ismissing(DataTable);
[row,col] = find(styleIndices);
s = uistyle("BackgroundColor",[1 0.6 0.6]);
addStyle(uit,s,"cell",[row,col]);
% Create graph for drag
tiledlayout(2,2)
nexttile(2)
title("Avg Drag")
xlabel("Deployment")
xticks([5,10,15])
hold on
for n = 1:length(Speeds)
plot([5 10 15],double(ExampleData(n+(2*(n-1)):n+(2*(n-1))+2,3)))
end
hold off
% Create graph for lift
nexttile(4)
title("Avg Drag")
xlabel("Deployment")
xticks([5,10,15])
hold on
for n = 1:length(Speeds)
plot([5 10 15],double(ExampleData(n+(2*(n-1)):n+(2*(n-1))+2,4)))
end
hold off
Here is what it displays:
Here is what I want it to display (doctored photo):
Is this possible to achieve?
I am also open to any advice on restructuring this more efficiently! Thanks in advance!
댓글 수: 0
채택된 답변
Adam Danz
2023년 3월 30일
편집: Adam Danz
2023년 3월 30일
Create a uigridlayout and put a uitable in it using the Layout property of uitable. To add the axes in the demo below, I added a uipanel on the right (invisible) and then I used tiledlayout() to add two axes that are within the uipanel.
Adjust the demo to your needs. I've added comments where you can tweek the sizes of each object within the grid.
T = table(randi(100,10,1),randi(5,10,1),rand(10,1),rand(10,1), ...
'VariableNames', {'Speed','Deployment','Drag','Lift'});
fig = uifigure();
uig = uigridlayout(fig,[1,4]); %create a 1x4 grid
uit = uitable(uig,'data',T);
uit.Layout.Row = 1;
uit.Layout.Column = [1,3]; % Uitable goes in the first 3 columns of the grid
uip = uipanel(uig,'BorderType','None');
uip.Layout.Row = 1;
uip.Layout.Column = 4; % uipanel goes in the 4th column.
tcl = tiledlayout(uip,2,1); % TileChartLayout goes in the uipanel, size 2x1
ax1 = nexttile(tcl); % Add a tile (axes) to the TiledChartLayout
plot(ax1, T.Deployment, T.Drag,'o')
title(ax1,'Drag')
xlabel(ax1,'Deployment')
box(ax1,'on')
ax2 = nexttile(tcl); % Add a tile (axes) to the TiledChartLayout
plot(ax2, T.Deployment, T.Lift,'o')
title(ax2,'Lift')
xlabel(ax2,'Deployment')
box(ax2,'on')
댓글 수: 2
Adam Danz
2023년 3월 30일
> it is good practice to be more specific with your holds
💯
Indeed! Thanks for pointing that out. You may have noticed that I specified parent handles in all of the graphics functions in my solution such as title(ax1,__), uipanel(uig,__). I think it's more readable and it eliminate the possibility of objects being applied to the wrong parent.
추가 답변 (1개)
Peter Perkins
2023년 4월 5일
편집: Peter Perkins
2023년 4월 5일
Mathew, I think you are making your life more difficult than it needs to be with all those strings. I get that you are trying to have a nice display for each speed, but it's making the computations pretty awkward, I think. You might be better off making your own pretty-print function just for display. In any case, with numeric variables:
speeds = [40 80 120];
Speed = repelem(speeds(:),3);
Drag = rand(9,1);
Lift = rand(9,1);
Strain = rand(9,1);
Deployment = repmat([5;10;15],3,1);
T = table(Speed,Deployment,Drag,Lift,Strain)
for sp = speeds
plot(T(T.Speed == sp,:),"Deployment","Drag");
hold on
end
hold off
I don't know if this helps at all, but variables in a table can have multiple columns, so this might be another way to see both your dimensions:
>> Deployment = Deployment(1:3);
>> Drag = reshape(Drag,3,3);
>> Lift = reshape(Lift,3,3);
>> Strain = reshape(Strain,3,3);
>> T = table(Deployment,Drag,Lift,Strain)
T =
3×4 table
Deployment Drag Lift Strain
__________ _____________________________ _____________________________ _______________________________
5 0.22592 0.4357 0.43021 0.97975 0.25806 0.26221 0.22175 0.31878 0.085516
10 0.17071 0.3111 0.18482 0.43887 0.40872 0.60284 0.11742 0.42417 0.26248
15 0.22766 0.92338 0.90488 0.11112 0.5949 0.71122 0.29668 0.50786 0.80101
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!