Can Matlab write the output directly in a word file

status = mkdir('D:\PK'); cd D:\PK
syms y(t) a b
eqn = diff(y,t,2) == a^2*y;
Dy = diff(y,t);
cond = [y(0)==b, Dy(0)==1];
ySol(t) = dsolve(eqn,cond)
save2word('Pks.doc',h); % save to word

1 Comment

What exactly do you want to save?
You have used "h" as an example but that does not corresponding to anything from your code.

Sign in to comment.

 Accepted Answer

You can write text with this kind of structure
text_to_write = 'My text';
fid = fopen('Pks.doc','w');
fprintf(fid,text_to_write);
fclose(fid);

8 Comments

@ Dear Florian
It seems your code is very close but I need the output "ySol(t)" to be printed in that word file.
"It seems your code is very close"
It is nowhere close: it just creates a textfile with a .doc extension.
Does this work ?
text_to_write = "ySol(t) = " + string(ySol(t));
fid = fopen('Pks.doc','w');
fprintf(fid,text_to_write);
fclose(fid);
"Does this work ?"
How do you expect that code to generate an Office OpenXML file or (as the OP requested) a binary Office file?
@Stephen23 Well, this way creates a .doc txt document that I can open in word like a 97-2003 word format. It might not be a neat solution, I agree, but at least it's super simple. Since we have no context on what Minati really wants to achieve, I don't think it is completely stupid.
I know this won't be a real word file, but I'm not sure what was intended here.
@MINATI PATRA If you need a real word file, use the next answer from Muhammad :)
"Well, this way creates a .doc document that I can open in word with a 97-2003 word format."
It creates a text file. What it creates is nothing like a MS Office binary file.
That you can open text files with MS Office is not a test of the file format.
The .DOC file extension is commonly used with the proprietary MS Office binary file format:
Of course it won't create an office binary file. I just proposed an easy way of going around the solution, as I expect the final purpose of this file is to be modified using Word anyway which in this case can be saved as a real office binary document.
As I just said in the previous comment, Muhammad answered this question properly.

Sign in to comment.

More Answers (1)

If you would like to create a Word document from MATLAB and write some output to it using ActiveX (only works on Windows):
% Ensure MATLAB is connected to a Word Application
wordApp = actxserver('Word.Application');
wordApp.Visible = true;
% Add a new document
doc = wordApp.Documents.Add;
% Write some text to the document
selection = wordApp.Selection;
selection.TypeText('This is the output text in the Word document.');
% You can also add more complex formatting, insert charts, tables, etc.
% For example, to create a heading and then a paragraph under it:
selection.TypeText('Heading 1');
selection.Style = 'Heading 1';
selection.TypeParagraph; % This creates a new paragraph
selection.TypeText('This is an example paragraph under the heading.');
% Insert a page break
selection.InsertBreak; % This inserts a page break
% Continue writing text or add other elements as needed
selection.TypeText('Text after the page break.');
% Save the document to a specific path
filePath = fullfile('D:\PK', 'MyWordDoc.docx');
doc.SaveAs2(filePath);
% Close the Word Application
doc.Close;
wordApp.Quit;
% Release the ActiveX server
delete(wordApp);
Make sure you have the necessary permissions to write files to the directory you're specifying, and that Word is installed on the system where you're running this script.
This script opens a Word application, creates a new document, writes some text to it, inserts a page break, adds more text, and then saves and closes the document.
Please replace 'D:\PK' and 'MyWordDoc.docx' with the actual path and filename where you want to save your Word document.
This is a basic example, and Word's ActiveX interface has a very wide range of features that you can use to format and work with your Word document programmatically from MATLAB.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

5 Comments

But his code is not complete, I think needs some modification for my case.
@MINATI PATRA, then ask the poster, specifying what the answer is lacking and what needs to be done further.
@MINATI PATRA Code is pretty self explanatory incase of any doubts you can share your specific requirements. Thank you.
@ Muhammad Actually, I can't interpret your code with mine which I have posted perhaps.
@ Muhammad
Please refer the code I have posted and make some necessary arrangement to run successfully.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!