Can Matlab write the output directly in a word file

30 views (last 30 days)
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
Dyuman Joshi
Dyuman Joshi on 11 Jan 2024
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

Florian Bidaud
Florian Bidaud on 11 Jan 2024
Edited: Florian Bidaud on 11 Jan 2024
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
Stephen23
Stephen23 on 11 Jan 2024
"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:
Florian Bidaud
Florian Bidaud on 11 Jan 2024
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)

Hassaan
Hassaan on 11 Jan 2024
Edited: Hassaan on 11 Jan 2024
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
MINATI PATRA
MINATI PATRA on 12 Jan 2024
@ Muhammad Actually, I can't interpret your code with mine which I have posted perhaps.
MINATI PATRA
MINATI PATRA on 12 Jan 2024
@ 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!