Hi, I'm trying to plot a 2d graph from a table in my workspace, but it only shows numbers in the command window and doesn't plot the graph. The first column is the time step and the second and third columns are temperature values.
Below is my code:
The table is called "battery_half" and is a 7201*3 table.
x1=battery_half(:,1);
y1=battery_half(:,2);
y2=battery_half(:,3);
plot(x1, y1, y2, 'LineWidth', 2.0)
xline(0, 'LineWidth', 1.0)
yline(0, 'LineWidth', 1.0)
xlabel('Flow time (sec)')
ylabel('Temperature (C)')
legend('Computational (0.5C-DC)','Experimental (0.5C-DC)')
ax=gca;
ax.Fontsize=12;
figure(gcf)

 채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 12월 1일
편집: Dyuman Joshi 2023년 12월 1일

1 개 추천

When you use parenthesis, (), on a table, the output is also a table.
Use curly brackets, {}, to access the data inside the table. For more information, refer to Access Data in Tables
% v v
x1=battery_half{:,1};
y1=battery_half{:,2};
y2=battery_half{:,3};
Also, the syntax to plot multiple graphs using a single plot() is
plot(x1, y1, x2, y2, ...)
If x2 is same as x1, use x1 instead. But both set of coordinates have to be specified for each plot.
plot(x1, y1, x1, y2, 'LineWidth', 2.0)
Additionally, the property is 'FontSize', so you need to capitalize the 's'.
And the last line of code is redundant, you can remove it.

댓글 수: 3

Thank you for your reply.
I edited the code like the following, but it still doesn't seem to plot properly : (
I also attached the table I'm trying to access the data from.
I would appreciate any advice, thanks!
x1=battery_half{:,1};
y1=battery_half{:,2};
y2=battery_half{:,3};
plot(x1, y1, x1, y2, 'LineWidth', 2.0)
xline(0, 'LineWidth', 1.0)
yline(0, 'LineWidth', 1.0)
xlabel('Flow time (sec)')
ylabel('Temperature (C)')
legend('Computational (0.5C-DC)','Experimental (0.5C-DC)')
ax=gca;
ax.FontSize=12;
Your data is in a numeric array.
Note that Table in MATLAB has a specific meaning i.e. it is a distinct data type - Tables
The plot appears with curves now.
Let me know if you still have some unresolved query.
load('battery_half.mat')
whos
Name Size Bytes Class Attributes ans 1x41 82 char battery_half 7201x3 172824 double cmdout 1x33 66 char
x1=battery_half(:,1);
y1=battery_half(:,2);
y2=battery_half(:,3);
plot(x1, y1, x1, y2, 'LineWidth', 2.0)
xline(0, 'LineWidth', 1.0)
yline(0, 'LineWidth', 1.0)
xlabel('Flow time (sec)')
ylabel('Temperature (C)')
legend('Computational (0.5C-DC)','Experimental (0.5C-DC)', 'Location', 'Best')
ax=gca;
ax.FontSize=12;
Dana Kang
Dana Kang 2023년 12월 3일
Oh that seems to work, thank you very much! : )

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

릴리스

R2023a

태그

질문:

2023년 12월 1일

댓글:

2023년 12월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by