Borrar filtros
Borrar filtros

How to make a text file from functions

1 visualización (últimos 30 días)
Chloe
Chloe el 22 de Abr. de 2024
Respondida: Sanju el 29 de Abr. de 2024
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
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

Respuestas (1)

Sanju
Sanju el 29 de Abr. de 2024
Hi Chloe,
To generate a text file using MATLAB functions, utilize the "fprintf" function to input the desired content into the file. It appears that the code you've written is functioning correctly; however, remember to close the file once the operation is complete.
Here's the updated code,
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
fclose(myFile);
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
This code will create a file named "myValues.txt" and write the desired content to it. Each line in the file will contain the values of i, j, myHypot, myPerim, and myArea separated by tabs.
Hope this helps!

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by