Read in numbers as text from excel
Show older comments
I have a spreadsheet with a mix of numbers and text. However, I need to read the numbers in as text to preserve the exact formatting used in the excel spreadsheet.The readtable(), readcell(), and xlsread() functions all automatically convert the numbers to a double format, even if it is formatted as text in the source spreadsheet. As a result, formatting information (e.g. scientific notation vs decimal) is lost.
I tried using detectImportOptions, then setting the VariableType, but they're still getting converted to double:
filename = 'Test_File.xlsx';
opts = detectImportOptions(filename);
opts = setvartype(opts,'char'); % Set all variable types to char
raw_data = readcell(filename,opts); % Numbers here are still converted to double
Is there a way to keep all excel data as text while reading it in to Matlab? Thanks for your help.
3 Comments
When I try it, one cell formatted as text is imported as char:
[N,T,R] = xlsread('test.xlsx','','','basic')
C = readcell('test.xlsx')
Are all of the cells text, or only some of them? If mixed: are data types consistent within each column? Please upload a sample file by clicking the paperclip button.
Are you using Windows with MS Office installed?
Note that if you want reliable data storage/transfer, avoid MS Excel formats:
Blake
on 24 Aug 2021
Stephen23
on 24 Aug 2021
"I do have excel installed, and I wish I could avoid Excel in this case!"
If you are using XLSREAD then use BASIC mode, as my last comment shows.
Answers (1)
IF in excel, a number with high precision is not stored with text format, it cannot be read by readcell with string format for high precision purpose but only be read with double precision, actually excel does the same way with matlab. So why do you force matlab to do that?
When you store a high precision number with text format in excel. then, matlab can read it as a string.
Usually when we write a high precision number in excel, we need to write '3.1415926535897932384626433832795 rather than directly write 3.1415926535897932384626433832795. Because the latter will lose precision in excel.
Categories
Find more on Spreadsheets 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!