How can I print multiple text blocks containing numerical values of a matrix with the fprintf command?

3 views (last 30 days)
Dear all,
I'm trying to write blocks of multiple text lines to a single text file by putting one block under the other. A single block needs to contain the following text and numerical values:
CASE i
NAME "Point load i"
NODAL
a FORCE 4 1.00000E+00
b FORCE 4 1.00000E+00
c FORCE 4 1.00000E+00
d FORCE 4 1.00000E+00
e FORCE 4 1.00000E+00
in which i needs to vary from 2 to 500, and the first column with node IDs (a b c d e representing integers) also changes from case to case.
In a first step I've put the case/name numbers in a 499x1-column vector and the node IDs in a 5x499-matrix:
N_l = 500;
N_t = 5;
NodeID = zeros(N_t,N_l-1);
NodeID(1,1) = 26;
NodeID(2,1) = 27;
NodeID(3,1) = 36;
NodeID(4,1) = 41;
NodeID(5,1) = 46;
for i = 1:N_t
for j = 2:N_l-1
NodeID(i,j) = NodeID(i,1) + (j-1)*25;
end
end
In the second step I tried to print the first three lines of the text block by using the following command lines:
Case = 2:1:N_l;
formatSpec = 'CASE %d\nNAME "Point load %d"\nNODAL\n';
fprintf(formatSpec,Case)
fileID = fopen('data.txt','w');
fprintf(fileID,'CASE %d\r\nNAME "Point load %d"\r\nNODAL\r\n',Case);
fclose(fileID);
However, Matlab returns the following text:
CASE 2
NAME "Point load 3"
NODAL
CASE 4
NAME "Point load 5"
NODAL
CASE 6
NAME "Point load 7"
...
The numbering of the case and point load seems to be wrong. Moreover, I don't know how to print the second part of text lines within the same text block. Is this still possible with the fprintf command?
Kind regards,
Jeremy
  1 Comment
Stephen23
Stephen23 on 24 Jun 2015
Edited: Stephen23 on 25 Jun 2015
Please
do
not
put
a
blank
line
between
every
line
of
your
code.
The code formatting works just fine without this. There is no need to make your life and our lives more difficult.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 24 Jun 2015
Edited: Stephen23 on 25 Jun 2015
The way you are creating NodeID is very inefficient: learn about code vectorization to avoid this. Here it is using a much faster and neater bsxfun call:
D = 25;
N = 500;
X = [26,27,36,41,46];
Y = D*(0:N-1)';
NodeID = bsxfun(@plus,X,Y);
And then printing to file, perhaps something like this:
fid = fopen('temp.txt','wt');
for k = 2:N
vec = +('a':'e'); % integers that can change between cases
fprintf(fid,'CASE %d\nNAME "Point load %d"\nNODAL\n',k,k);
fprintf(fid,'%c FORCE 4 %.5E\n',[vec;NodeID(k,:)]);
end
fclose(fid);
Which generates this file (obviously this is the first few lines only):
CASE 2
NAME "Point load 2"
NODAL
a FORCE 4 5.26000E+02
b FORCE 4 5.27000E+02
c FORCE 4 5.36000E+02
d FORCE 4 5.41000E+02
e FORCE 4 5.46000E+02
CASE 3
NAME "Point load 3"
NODAL
a FORCE 4 1.02600E+03
b FORCE 4 1.02700E+03
c FORCE 4 1.03600E+03
... ETC
You do not indicate what values should get printed in the blocks, so I assumed that these should be the NodeID values.
  1 Comment
Stephen23
Stephen23 on 25 Jun 2015
Edited: Stephen23 on 25 Jun 2015
Based on your comment, this might do what you are after:
D = 25;
N = 500;
X = [26,27,36,41,46];
Y = D*(0:N-1)';
NodeID = bsxfun(@plus,X,Y);
%
vec = ones(1,5);
fid = fopen('temp.txt','wt');
for k = 1:size(NodeID,1)
fprintf(fid,'CASE %d\nNAME "Point load %d"\nNODAL\n',1+k,1+k);
fprintf(fid,'%d FORCE 4 %.5E\n',[NodeID(k,:);vec]);
end
fclose(fid);
And it generates this file:
CASE 2
NAME "Point load 2"
NODAL
26 FORCE 4 1.00000E+00
27 FORCE 4 1.00000E+00
36 FORCE 4 1.00000E+00
41 FORCE 4 1.00000E+00
46 FORCE 4 1.00000E+00
CASE 3
NAME "Point load 3"
NODAL
51 FORCE 4 1.00000E+00
52 FORCE 4 1.00000E+00
61 FORCE 4 1.00000E+00
66 FORCE 4 1.00000E+00
... ETC
But if you do not require the variable NodeID for any other calculations, then we can simplify even more by not defining it at all:
D = 25;
N = 500;
X = [26,27,36,41,46];
vec = ones(1,5);
fid = fopen('temp.txt','wt');
for k = 2:N
fprintf(fid,'CASE %d\nNAME "Point load %d"\nNODAL\n',k,k);
fprintf(fid,'%d FORCE 4 %.5E\n',[X+25*(k-2);vec]);
end
creates exactly the same file.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!