Read text file from function
Mostrar comentarios más antiguos
Hi!
Im trying an old course matlab program and it read data from a text file but it isnt working and i cant figure out whats wrong. Im not familiar with reading in files from matlab functions, hope someone can help me out? Please see attached files. Can someone see if the command below will function as it should?
fid = fopen('truss2D.inp','r');
[node,element,Dzero,Fs,elem_force,Nn,Ne] = read_input(fid);
fclose(fid);
The text files has the following text:
*NODE [Node number, X-coordinate, Y-coordinate]
1, 0.0, 0.0
2, 4.0, 3.0
3, 0.0, 3.0
*ELEMENT [Element no., Node 1, Node 2, E, A]
1, 1, 2, 200e9, 25.0e-4
2, 2, 3, 200e9, 4.0e-4
3, 1, 3, 200e9, 6.0e-4
*ZeroDiplacement [Node number, D.O.F.]
1,1
2,1
2,2
3,2
*PointForce [Node number, D.O.F., force value]
1,2, -5.0e4
*VolumeForce [Element number, q0, q1, q2]
3, -1.0e7, 0, 0
*END
% This routine reads in-put data from the file "truss2D.inp"
function [node,element,Dzero,Fs,elem_force,Nn,Ne] = read_input(fid)
data = [];
%
% Node numbers and coordinates
data = str2num(fopen(fid));
data = str2num(fopen(fid));
while (length(data) > 0)
Nnum = round(data(1));
node(Nnum,1:2) = data(2:3);
data = str2num(fopen(fid));
end
Nn = length(node(:,1));
%
% Elements (connectivity, E & A)
data = str2num(fopen(fid));
while (length(data) > 0)
Enum = round(data(1));
element( Enum, 1:4) = data(2:5);
data = str2num(fopen(fid));
end
Ne = length(element(:,1));
%
% Zero displacement Boundary Conditions
Dzero = zeros(2*Nn,1);
data = str2num(fopen(fid));
while (length(data) > 0)
if (data(2) == 1)
Dzero(2*data(1)-1) = 1;
elseif (data(2) == 2)
Dzero(2*data(1)) = 1;
end
data = str2num(fopen(fid));
end
%
% Applied point forces
Fs = zeros(2*Nn,1);
data = str2num(fopen(fid));
while (length(data) > 0)
if (data(2) == 1)
Fs(2*data(1)-1) = data(3);
elseif (data(2) == 2)
Fs(2*data(1)) = data(3);
end
data = str2num(fopen(fid));
end
%
% Applied volume forces
elem_force = zeros(Ne,4);
data = str2num(fopen(fid));
while (length(data) > 0)
elem_force(data(1),:) = data;
data = str2num(fopen(fid));
end
return
%*************************************************************
2 comentarios
Mario Malic
el 16 de En. de 2021
Editada: Mario Malic
el 16 de En. de 2021
We can't help if you don't provide us with the read_input function.
Here's an example how to read the file, you'll need to process it further to get the required outputs.
function fileData = ReadFile(inputFilePath)
% Function reads supplied filepath and outputs it as the cell fileData
fileData = splitlines(fileread(inputFilePath));
end
Daniel Arvidsson
el 16 de En. de 2021
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Standard File Formats en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!