Read equations from MS Word
24 views (last 30 days)
Show older comments
Benjamin Pehlivanlar
on 4 Nov 2022
Commented: Benjamin Pehlivanlar
on 8 Nov 2022
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1181233/image.png)
Thanks in advance for the help.
Br
2 Comments
Daniel Neubauer
on 4 Nov 2022
is it possible to save your .docx file as a plain text file with *.m extension? then you could just run it
Shivam Malviya
on 7 Nov 2022
Hi Benjamin,
Could you share the word document that contains the equation?
Thanks,
Shivam
Accepted Answer
Shivam Malviya
on 7 Nov 2022
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
2 Comments
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!