Read text file from function

5 visualizzazioni (ultimi 30 giorni)
Daniel Arvidsson
Daniel Arvidsson il 16 Gen 2021
Commentato: Rik il 16 Gen 2021
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 Commenti
Mario Malic
Mario Malic il 16 Gen 2021
Modificato: Mario Malic il 16 Gen 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
Daniel Arvidsson il 16 Gen 2021
Youre right Mario, i forgot. Its been provided now, thanks!

Accedi per commentare.

Risposta accettata

Mario Malic
Mario Malic il 16 Gen 2021
Hi again,
here's a script that processes the input files. There are two parameters missing n, Ne. Implement these and convert this script to function.
Note: it uses ReadFile function.
clc;
clear;
format long g % Element contains 1E11 number, other elements are not displayed nicely without this
fileData = ReadFile('truss2D_inp.txt');
keywordGroup = contains(fileData, '*'); % Returns logical array where character '*' is found
keywordStrings = {'Node', 'Element', 'ZeroDisplacement', ...
'PointForce', 'VolumeForce'};
emptyVals = cell(size(keywordStrings));
structPairs = reshape([keywordStrings;emptyVals], 1, []); % Reshape into name-value pair for struct
inputData = struct(structPairs{:});
% Looping over fileData
jj = 0;
for ii = 1 : length(fileData)
if keywordGroup (ii) % if '*' is found, increment the keywordStrings index
jj = jj + 1;
else
inputData(ii).(keywordStrings{jj}) = str2num(fileData{ii});
end
end
% Processed data
Node = vertcat(inputData.(keywordStrings{1}));
finElement = vertcat(inputData.(keywordStrings{2}));
fixedDisp = vertcat(inputData.(keywordStrings{3}));
pForce = vertcat(inputData.(keywordStrings{4}));
volumeForce = vertcat(inputData.(keywordStrings{5}));
% missing N and NE
function fileData = ReadFile(inputFilePath)
% Function reads supplied filepath and outputs it as the cell fileData
fileData = splitlines(fileread(inputFilePath));
end
  1 Commento
Rik
Rik il 16 Gen 2021
An alternative for this ReadFile function is my readfile function, which should be a bit more robust that what you can achieve with a oneliner.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by