Info

This question is closed. Reopen it to edit or answer.

Can't figure out how to properly format the second string in alternating strings of data in a new file

1 view (last 30 days)
I am trying to write code that performs the following tasks:
  1. Opens a text file with two large sets of data separated by a keyword (this executes correctly)
  2. Scans the first set, pulls the first string, and writes it to the new file in the same format as the original, then writes a new line (this executes correctly)
  3. Scans the second set to pull the values with the corresponding node ids to the first data set, and write them in order below the first line in the new file
  4. Alternate these lines until the file is completely read.
I have tried several variations of code but the best I can get alternating lines with matching element ids instead of matching node ids.
clear all
close all
n_nodes = 4;
part_number = 1;
fid = fopen('Shell_Coarse_1mod.k');
fid2 = fopen('Shell_Coarse_1ordered.k', 'wt');
while strcmp(fgetl(fid),'*KEYWORD') == 0
end
while strcmp(fgetl(fid), '*ELEMENT_SHELL') == 0
end
fprintf(fid2,'*KEYWORD\n');
fprintf(fid2,'*ELEMENT_SHELL_THICKNESS\n');
tline=fgetl(fid);
f_str = '%n,';
for i = 1:n_nodes
f_str = strcat(f_str,'%n,');
end
f_str(length(f_str))=[];
f_str_1 = '%n, %f64, %f64, %64f \n';
f_str_out_1 = f_str_1;
f_str_2 = '%n,%f64';
f_str_out_2 = '%f64, %f64 \n';
while ((tline ~= -1) & (strcmp(tline(1),'*')) == 0)
D = textscan(tline,f_str);
fprintf(fid2,'%d, %d', D{1}, part_number);
for i = 1:n_nodes
fprintf(fid2,', %d', D{i+1});
end
fprintf(fid2,'\n');
C = textscan(tline,f_str_1);
fprintf(fid2, f_str_out_1, '%d', C{1}, C{2});
tline = fgetl(fid);
E = textscan(tline,f_str_2);
fprintf(fid2, f_str_out_2, E{1}, E{2});
end
fprintf(fid2,'\n');
fprintf(fid2,'*END');
fclose(fid);
fclose(fid2);
What I'm getting is
eid1, pid, n1, n2, n3, n4
nid1, thickness
eid2, pid, n5, n6, n7, n8
nid2, thickness
When I want
eid1, pid, n1, n2, n3, n4
n1thick, n2thick, n3thick, n4thick
eid2, pid, n5, n6, n7, n8
n5thick, n6thick, n7thick, n8thick
and so on.
The attached file is the source file, with the first data set in the form
eid1, pid, n1, n2, n3, n4
eid2, pid, n5, n6, n7, n8
and the second data set in the form
nid1, thickness
nid2, thickness
Any help would be appreciated.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!