MATLAB Answers

How can I read a column from a word file?

52 views (last 30 days)
SSG_newbiecoder
SSG_newbiecoder on 9 Mar 2018
Edited: Akira Agata on 9 Mar 2018
Hello, I have a word document like this and I want to read only the Type column and save it as a csv file. Is it possible in matlab?
Time Sample # Type Sub Chan Num Aux
0:00.144 52 + 0 0 0 (N
0:00.375 135 N 0 0 0
0:01.017 366 N 0 0 0
0:01.683 606 N 0 0 0
0:02.336 841 N 0 0 0
0:03.006 1082 N 0 0 0
0:03.686 1327 N 0 0 0
0:04.339 1562 N 0 0 0
0:04.983 1794 N 0 0 0

  0 Comments

Sign in to comment.

Answers (1)

Akira Agata
Akira Agata on 9 Mar 2018
Edited: Akira Agata on 9 Mar 2018
The better way is to convert your data to text (or CSV) file first by MS Word...
But, anyway, if you have to read from MS Word file, the following code can do that.
% Full path to the MS Word file
filePath = fullfile(pwd,'yourData.docx');
% Read MS Word file using actxserver function
word = actxserver('Word.Application');
wdoc = word.Documents.Open(filePath);
txt = wdoc.Content.Text;
Quit(word)
delete(word)
% Extract 'Type' column and save as CSV file
c = textscan(txt,'%s%f%s%f%f%f%s','HeaderLines',1);
csvwrite('Type.csv',c{2});
If you have Text Analytics Toolbox, you can do this more easily, like:
% Full path to the MS Word file
filePath = fullfile(pwd,'yourData.docx');
% Read MS Word file using extractFileText function
str = extractFileText(filePath)
str = strrep(str,[newline newline],newline);
% Extract 'Type' column and save as CSV file
c = textscan(str,'%s%f%s%f%f%f%s','HeaderLines',1);
csvwrite('Type.csv',c{2});

  0 Comments

Sign in to comment.

Sign in to answer this question.