Using readtable with Word document and special formatting (superscripts)

조회 수: 4 (최근 30일)
AR
AR 2024년 3월 7일
댓글: AR 2024년 10월 18일
When reading a table from a Word document, readtable seems to treat superscript content in the same way as other content.
For example, if a row in a table contains , readtable returns Lunar21, which is a problem. Superscripts are often used to point to footnotes. Is there an option for readtable to ignore superscript and other such characters, or any other workaround?
  댓글 수: 6
AR
AR 2024년 3월 7일
Thanks for the tip on attaching. Here's an example, although there are many more tables that are much larger and harder to work with.
AR
AR 2024년 3월 8일
Nevermind my original request. Although I still don't know how to directly remove special formatting while importing using readtable (if it's possible at all), I did manage to get the actxserver method working. Thanks.

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

채택된 답변

Shlok
Shlok 2024년 10월 16일
Hi AR,
I understand that you are encountering issues while reading text with superscript characters using the “readtable” function. To address this issue, I would suggest utilizing an ActiveX session with Microsoft Word. This approach will allow you to remove any superscript characters before attempting to read the table. Refer the following steps to do so:
  • Using MATLAB, start an ActiveX session with Word and load the document.
wordApp = actxserver('Word.Application');
wordApp.Visible = true; % You can keep the Microsoft Word visible to debug in case of any errors
doc = wordApp.Documents.Open('path_to_Temp_tableAttempt.docx');
  • Now loop through the tables and, within each cell, iterate through each character to check for superscripts. If a superscript character is encountered, delete it:
for i = 1:doc.Tables.Count
table = doc.Tables.Item(i);
for row = 1:table.Rows.Count
for col = 1:table.Columns.Count
try
cell = table.Cell(row, col);
cellRange = cell.Range;
% Loop through each character in the cell
for charIndex = cellRange.Characters.Count:-1:1
char = cellRange.Characters.Item(charIndex);
% Check if the character is superscript
if char.Font.Superscript
% Remove the superscript character
char.Delete();
end
end
% To handle merged columns
catch ME
disp(['Error processing cell at row ' num2str(row) ', column ' num2str(col) ': ' ME.message]);
end
end
end
end
  • Then save this updated file and close the ActiveX session.
doc.SaveAs2('path_to_updated_Temp_tableAttempt.docx ');
doc.Close();
wordApp.Quit();
By following these steps, you will be able to create an updated document free of superscript characters, making it easier to read the table using the “readtable” function.
To know more about actxserver” function you can refer to the following MATLAB documentation link:
Hope this helps.
  댓글 수: 3
AR
AR 2024년 10월 18일
Thank you. I will try this out.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by