How to store fprintf statements into a txt file?
Show older comments
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;
end
end
plot(x,force)
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('<ANSYS_EnggData>\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);]
writetable(Text,'Test.txt')
winopen('Test.txt')
2 Comments
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....
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with MuPAD in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!