MATLAB Answers

How to store fprintf statements into a txt file?

11 views (last 30 days)
Robert  Flores
Robert Flores on 25 Mar 2019
Answered: Guillaume on 25 Mar 2019
Hello everyone,
I am trying to write an XML file that I can upload into ANSYS for a model I am trying to simulate. I am using fprintf statements to format the ouput, such that it maps out like an XML file. But, I wanna test what I am doing by outputing it into a .txt file; to see what it looks like. Also, I have been trying to use the writetable function, but maybe that is what is giving me an issue. If anyone can please help me it would be much appreciated, thanks.
Note: tt and ff are to make the values in my row matrix have commas in between each value. Also, I want to have the values in tt immediately follow the statement in the last fprintf statement, so it looks like the following:
<Data format="float">0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.011,0.012,0.013....
clc, clear, close all
time = 0:0.001:9; x=time;
force = 1000*sin(x);
for i=1:numel(x)
if x(i) <= pi
force(i) = 1000*sin(x(i));
elseif (x(i) > pi)
force(i) = 0;
axis([0 5 0 1000])
title('Force v Time');xlabel('Time, measured in sec');ylabel('Force, measured in N')
Time = time';
tt = regexprep(num2str(time),'\s+',',');
Force = force';
ff = regexprep(num2str(force),'\s+',',');
Text = [fprintf('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n');
fprintf(' <MaterialData/>\n');
fprintf(' <ConvectionData/>\n');
fprintf(' <LoadVariationData>\n');
fprintf(' <MatML_Doc>\n');
fprintf(' <LoadVariation>\n');
fprintf(' <BulkDetails>\n');
fprintf(' <Name>Force</Name>\n');
fprintf(' <Form>\n');
fprintf(' <Description></Description>\n');
fprintf(' </Form>\n');
fprintf(' <PropertyData property="pr1">\n');
fprintf(' <Data format="float">',tt);]


dpb on 25 Mar 2019
I've never written/specifically read an XML file in my life but look at
doc xmlwrite % and friends
Looks like TMW has built some tools for the purpose...
dpb on 25 Mar 2019
If the above doesn't prove to be of real benefit, it's not difficult to write--just write the stuff you want directly to the file; you don't need to go thru writetable or any other intermediaries...
For the specific question, it would be simply
<Data format="float">0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.011,0.012,0.013....

Sign in to comment.

Accepted Answer

Guillaume on 25 Mar 2019
  • You can pass a file identifier to fprintf (after you've opened the file), e.g.:
fid = fopen('Test.txt', 'wt');
fprintf(fid, 'Some text');
  • You seem to be confusing fprintf with sprintf. The return value fprintf is the number of character written. So, at the end your Text variable is just an array of numbers: the number of characters you passed to each fprintf call. sprintf on the other hand, returns a char array. However, here you'd be better off just calling strjoin:
Text = strjoin(...
'<?xml version="1.0" encoding="UTF-8" standalone="no" ?>', ...
'<ANSYS_EnggData>', ...
'\n'); %join all char vectors with newlines
  • writetable requires a table as the first input. A char array is not a table. Since writetable is a class member, most likely matlab will tell you the function does not exist a
  • s it is only in scope when one of the arguments is a table
In any case, rather than attempting to write your xml (with all the potential bugs that it entails), you should be using xmlwrite which is guaranteed to generate valid xml:
doc = com.mathworks.xml.XMLUtils.createDocument('ANSYS_EnggData');
root = doc.getDocumentElement;
loadvardata = root.appendChild(doc.createElement('LoadVariationData'));
matml = loadvardata.appendChild(doc.createElement('MatML_Doc'));
loadvar = matml.appendChild(doc.createElement('LoadVariation'));
bulkdetail = loadvar.appendChild(doc.createElement('BulkDetails'));
name = bulkdetail.appendChild(doc.createElement('Name'));
form = bulkdetail.appendChild(doc.createElement('Form'));
pdata = doc.createElement('PropertyData');
pdata.setAttribute('property', 'pr1');
data = doc.createElement('Data');
data.setAttribute('format', 'float');
xmlwrite('Test.xml', root);


Sign in to comment.

More Answers (0)

Translated by