How to plot from .txt file

16 次查看(过去 30 天)
Chloe
Chloe 2024-4-22
编辑: akshatsood 2024-4-22
How do I plot the 2D and mesh graphs asked for if this is the code I'm using?
%start with a right triangle of length x = 1 and y = 1
%calculate all values (hypotenuse, perimeter, and area)
%for combinations of x = 1:5 and y = 1:5
%Print all of those as a table to a .txt file
%output should look something like this
%{
x y h p a
1 1 1.4 3.41 0.5
1 2 2.24 5.24 1
.....
.....
..... etc
%}
% using the data from the table
% plot a line (2d) graph of the perimeter(x axis) vs area (y axis)
% plot a mesh (3d) graph with x, y (respectively), and h on the 'z' axis
x = 1
y = 1
myFile = fopen('myValues.txt', 'w')
fprintf(myFile, "X Length\tY Length\t Hypot Length\t\tPerimeter\t\tArea\n")
for i = 1:5
for j = 1:5
myHypot = hValue(i,j)
myPerim = pValue(i,j)
myArea = aValue(i,j)
fprintf(myFile, string(i) + "\t\t\t" + string(j) + "\t\t\t\t" + string(myHypot) + "\t\t\t" + string(myPerim) + "\t\t\t" + string(myArea) + "\n")
end
end
plot(myPerim,myArea)
function hValue = hValue(xValue,yValue)
hValue = sqrt(xValue.^2+yValue.^2);
end
function pValue = pValue(xValue,yValue)
pValue = (xValue + yValue + (sqrt(xValue.^2+yValue.^2)));
end
function aValue = aValue(xValue,yValue)
aValue = 0.5*xValue*yValue;
end

回答(1 个)

akshatsood
akshatsood 2024-4-22
编辑:akshatsood 2024-4-22
Hi @Chloe,
I understand that you want to store a set of data into a TXT file and plot a 2D and 3D mesh using it. To achieve your goal, you need to store the calculated values in arrays so that you can plot them later. Your current code only writes to a file and attempts to plot using the last calculated myPerim and myArea, which won't work for plotting all the values. Here's a revised version of your code
% initialize arrays to store the values for plotting
perimeters = [];
areas = [];
x_vals = [];
y_vals = [];
hypots = [];
myFile = fopen('values.txt', 'w');
% format the file as stated in the question
fprintf(myFile, "x\t y\t h\t p\t a\n");
for i = 1:5
for j = 1:5
myHypot = hValue(i,j);
myPerim = pValue(i,j);
myArea = aValue(i,j);
% writing to the TXT file
fprintf(myFile, "%d\t\t\t%d\t\t\t%.2f\t\t\t%.2f\t\t\t%.2f\n", i, j, ...
myHypot, myPerim, myArea);
% store values for plotting
perimeters = [perimeters, myPerim];
areas = [areas, myArea];
hypots = [hypots, myHypot];
end
end
fclose(myFile);
% plot the 2D graph of perimeter vs. area
figure;
plot(perimeters, areas, 'o-');
xlabel('Perimeter');
ylabel('Area');
title('Perimeter vs. Area');
% plot the mesh with x, y, and hypotenuse
figure;
[X, Y] = meshgrid(1:5, 1:5);
mesh(X, Y, reshape(hypots, [5, 5]));
xlabel('X Length');
ylabel('Y Length');
zlabel('Hypotenuse Length');
title('Mesh plot of Hypotenuse Length');
% function to compute the hypotenuse
function hValue = hValue(xValue,yValue)
hValue = sqrt(xValue.^2 + yValue.^2);
end
% function to compute the perimeter
function pValue = pValue(xValue,yValue)
pValue = xValue + yValue + sqrt(xValue.^2 + yValue.^2);
end
% function to determine the area
function aValue = aValue(xValue,yValue)
aValue = 0.5 * xValue * yValue;
end
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by