Can Matlab write the output directly in a word file

137 visualizzazioni (ultimi 30 giorni)
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 Commento
Dyuman Joshi
Dyuman Joshi il 11 Gen 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.

Accedi per commentare.

Risposta accettata

Florian Bidaud
Florian Bidaud il 11 Gen 2024
Modificato: Florian Bidaud il 11 Gen 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 Commenti
Stephen23
Stephen23 il 11 Gen 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 il 11 Gen 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.

Accedi per commentare.

Più risposte (1)

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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by