Read equations from MS Word
20 次查看(过去 30 天)
显示 更早的评论
Dear all,
I am trying to import all the formulas in a Word document into MATLAB (plain text). The goal would then be to make a script that computes these imported formulas with the data I have.
I have been looking for a solution without any luck. I have tried with actxserver, but it seems like the wdoc.OMaths.ConvertToNormalText function does not exist in MATLAB.
word = actxserver('Word.Application');
wdoc = word.Documents.Open("C:\Users\pehlivanlar\Documents\test.docx");
size(wdoc.OMaths)
wdoc.OMaths.ConvertToNormalText
Unrecognized function or variable 'ConvertToNormalText'.
The original equations were created using Equation Editor 3.0, but I can easily convert them to Office Math ML Format:
Thanks in advance for the help.
Br
2 个评论
Daniel Neubauer
2022-11-4
is it possible to save your .docx file as a plain text file with *.m extension? then you could just run it
Shivam Malviya
2022-11-7
Hi Benjamin,
Could you share the word document that contains the equation?
Thanks,
Shivam
采纳的回答
Shivam Malviya
2022-11-7
Hi Benjamin,
According to my understanding, you are trying to do the following;
- Convert word document containing equations into plain text.
- Perform operations on the data according to the equations in the text file.
The word document file that contains equations is as below;
x.^1.5 + log10(x)
2*x + cos(x)
For converting the word document into a text file, the following script can be used;
% File paths
wordFile = [pwd filesep 'test.docx'];
txtFile = [pwd filesep 'test.txt'];
% Start the word application
word = actxserver('Word.Application');
% Get the document handle
document = word.Documents.Open(wordFile);
% Convert to text file
document.SaveAs2(txtFile, 2)
% Close the document
document.Close;
% Close the application
word.Quit;
% Print statement
disp("Converted successfully to text!!");
For operating on the data, according to the equations in the text file, the following script can be used;
% Import the text file that contains the formula
strEquations = fileread('test.txt');
% Process the string from the text file
strEquations = strsplit(strEquations, newline);
% Create a function handle that converts a string to a function handle
strToFuncHndl = @(x) str2func(['@(x)' x]);
% Convert each string equations into the function handles
equations = cellfun(strToFuncHndl, strEquations(1:end-1), 'UniformOutput',false);
% Create data
x = 1:10;
% Input data to the equations
disp("Output to the first equation");
equations{1}(x)
disp("Output to the second equation");
equations{2}(x)
Hope this helps.
Thanks,
Shivam Malviya
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!