Can Matlab write the output directly in a word file

조회 수: 33 (최근 30일)
MINATI PATRA
MINATI PATRA 2024년 1월 11일
댓글: MINATI PATRA 2024년 1월 12일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by