How can I read a column from a word file?

21 次查看(过去 30 天)
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

回答(1 个)

Akira Agata
Akira Agata 2018-3-9
编辑:Akira Agata 2018-3-9
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});

类别

Help CenterFile Exchange 中查找有关 Text Analytics Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by