Can Matlab write the output directly in a word file

93 次查看(过去 30 天)
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 个评论
Dyuman Joshi
Dyuman Joshi 2024-1-11
What exactly do you want to save?
You have used "h" as an example but that does not corresponding to anything from your code.

请先登录,再进行评论。

采纳的回答

Florian Bidaud
Florian Bidaud 2024-1-11
编辑:Florian Bidaud 2024-1-11
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 个评论
Stephen23
Stephen23 2024-1-11
"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 2024-1-11
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.

请先登录,再进行评论。

更多回答(1 个)

Hassaan
Hassaan 2024-1-11
编辑:Hassaan 2024-1-11
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 个评论
MINATI PATRA
MINATI PATRA 2024-1-12
@ Muhammad Actually, I can't interpret your code with mine which I have posted perhaps.
MINATI PATRA
MINATI PATRA 2024-1-12
@ Muhammad
Please refer the code I have posted and make some necessary arrangement to run successfully.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by