function eMat = import_elements_from_k_file(k_file_to_read)

% Loops through a .K file. Identifies the *NODE keyword and collects
% the nodal ID and x,y,z in a matrix for further processing.
%
% Struggles with multiple *NODE keywords directly after one another.
%
% INPUTS:   k_file_to_read    - String, name of file to create.
%
% OUTPUTS:  eMat              - [n x 4] matrix with element connectivity data.
%
% created by: ABR
% date : 2024-04-18
%

fileID = fopen(k_file_to_read);
tline = fgetl(fileID);
write_row = 0;
true_flag = false;
eMat = [];
while ischar(tline)
  tline = fgetl(fileID);
  if length(tline) == 14
    if strcmp(tline(1:14),'*ELEMENT_SHELL')
      true_flag = 1;
      while length(tline) > 49 || true_flag
        true_flag = 0;
        tline = fgetl(fileID);

        if tline(1) ~= '$' && tline(1) ~= '*'
          write_row = write_row + 1;
          eMat(write_row, 1) = str2double(tline(1:8));   % Element index
          eMat(write_row, 2) = str2double(tline(9:16));  % Part reference
          eMat(write_row, 3) = str2double(tline(17:24)); % N1
          eMat(write_row, 4) = str2double(tline(25:32)); % N2
          eMat(write_row, 5) = str2double(tline(33:40)); % N3
        end
      end
    end
  end
end
fclose(fileID);


