Printing answers in a word doc

19 views (last 30 days)
Hello community,
I am trying to write a code that prints the solutions of system of nonlinear equations into a word doc. I wrote a code that successfully solves for the variables but I am having trouble printing these values into a word doc.
I created the following code:
function F = SL10B(x)
Lab = .1;
Lbc = .6;
Lcd = .3;
Lad = .4;
rabx = Lab*cosd(0);
raby = Lab*sind(0);
F = [ (x(1))^2 + (x(2)^2)- Lbc^2;
(x(3))^2 + (x(4))^2 - Lcd^2;
raby + x(2) + x(4);
rabx + x(1) + x(3) - Lad];
fid = fopen('Solutions.doc','w');
fprintf(fid,'iteration x1 x2 x3 x4 \n');
fprintf(fid,'_______________________________________________________\n')
tab=[x(1);x(2);x(3);x(4)];
fprintf(fid,'%5d %9.2f %8.3f %8.3f %12.4f\n', tab);
end
A word doc opens but nothing is printed on it. How could I fix this?
Thank you.

Accepted Answer

Geoff Hayes
Geoff Hayes on 18 Oct 2015
Fabiano - while you have given the extension of doc to your file, it won't be a Word document that you are creating but just at text file. Is this sufficient?
As for the reason as to why the file is empty, it is most likely because you haven't closed the file that is identified by fid. Use fclose to close the file once you have finished writing all text to the file as
fclose(fid);
  2 Comments
Fabiano Da Mota
Fabiano Da Mota on 18 Oct 2015
Edited: Fabiano Da Mota on 18 Oct 2015
Geoff Hayes,
Thank you very much for your answer. That worked.
How would I be able to solve for the variables, x(1),x(2).. and print the answers on the the text file for the range theta = 0:30:360? I would like to have MatLab print the solutions for the given values of theta.Theta would range from 0 to 360 degrees in intervals of 30 degrees. I tried writing the following code, but I keep getting errors.
function F = SL10B(x)
Lab = .1;
Lbc = .6;
Lcd = .3;
Lad = .4;
theta = 0:30:360; % Range for theta
rabx = Lab*cosd(theta);
raby = Lab*sind(theta);
F = [ (x(1))^2 + (x(2)^2)- Lbc^2;
(x(3))^2 + (x(4))^2 - Lcd^2;
raby + x(2) + x(4);
rabx + x(1) + x(3) - Lad];
fid = fopen('Solutions1.doc','w');
fprintf(fid,'th x1 x2 x3 x4 \n');
fprintf(fid,'_______________________________________________________\n)
tab=[th ;x(1);x(2);x(3);x(4)];
fprintf(fid,'%5d %9.f %8.3f %8.3f %12.4f %12.4f\n', tab);
fclose(fid);
Thank you once again.
Geoff Hayes
Geoff Hayes on 18 Oct 2015
Fabiano - x appears to be an input to your function SL10B so it isn't clear to me why you trying to solve for x. This also seems like homework assignment...
Please post this as a separate question, describe what you are trying to do, and include the errors that you observe.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 18 Oct 2015
Consider one of these options:
  1. Using PUBLISH to publish your script to a Microsoft Word document.
  2. Using the Notebook interface.
  3. Controlling Microsoft Word via its COM interface and invoke the appropriate methods to write to the document.

Categories

Find more on Manage Products in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!