How to prevent readtable converting numbers to date automatically when importing data? (and/or) How to import multiple data ranges with readtable?

14 views (last 30 days)
Hello!
I ran into an issue using readtable with excel spreadsheets that contain data with mixed formats. My main data is basically a mix of dates and normal numbers. So for example the first three rows of each column contain dates followed by one row containing a plain number, then two lines with dates, one line plain number and so on.
When I use readtable to import, it automatically converts the plain numbers to dates (for example 10 -> 10-01-1900). Does anyone know how to prevent readtable from doing this automatic convertion?
I played around with readtable a bit but so far the only instances I found when readtable does not convert is when I import the rows containing the numbers seperately. But given the arrangement of the data in my files, I'd have to import each row containing normal numbers seperately, since readtable doesn't allow multiple ranges (would have to do the same for the rows containing dates). I'd rather want to avoid doing this.
So if anyone knows how to prevent readtable automatically converting my data OR how to import different rows of data simoultanously using readtable, I'd be most grateful.
  6 Comments
Harald
Harald on 12 Jan 2024
I had missed that you are using R2020b. I don't have that release installed but confirm that things are tricky with R2020a. You can actually have MATLAB do the swapping of rows and columns for you and get a nice table:
[~, ~, raw] = xlsread("test_nurDaten.xlsx");
xlswrite("temp.xlsx", raw');
T = readtable("temp.xlsx");
The code I suggested earlier works in R2023b and is preferable in that release, since it does not require the writing of a temporary file.
data = readcell("test_nurDaten.xlsx")';
T = cell2table(data);
Thus if that's an option, I recommend upgrading to the latest release.
Best wishes,
Harald
V.
V. on 17 Jan 2024
I managed to upgrade my version to the newest release (including swapping the language from German to English) and now readcell works just fine. Not sure if it's just the newest release or swapping the language solved my issue though (I think my initial issue with readcell was a lot due to language issues, but I can't be sure).
Anyways. Thanks for the help and tip with the version.

Sign in to comment.

Answers (1)

Hassaan
Hassaan on 12 Jan 2024
1. Specify Data Types for Each Column
You can instruct readtable to import each column as a specific data type. This can be done using the 'Format' parameter. Since your data is a mix of dates and numbers, you might consider importing the entire column as strings ('%s'). Afterward, you can programmatically convert the strings to the appropriate data type (dates or numbers) based on your knowledge of the data structure.
opts = detectImportOptions('yourfile.xlsx');
opts = setvartype(opts, 'char'); % or 'string', depending on your MATLAB version
data = readtable('yourfile.xlsx', opts);
% Then, programmatically convert cells to numbers or dates
for i = 1:height(data)
for j = 1:width(data)
if % your condition to check if it's a date or number
% convert data{i, j} to date or number
end
end
end
2. Read the Entire Data as Text and Convert Afterwards
Another approach is to read the entire data as text and then use custom logic to parse each cell, converting them to numbers or dates as appropriate.
opts = detectImportOptions('yourfile.xlsx', 'TreatAsEmpty', {'NA', 'na'});
opts = setvartype(opts, repmat({'char'}, 1, width(opts))); % read everything as text
data = readtable('yourfile.xlsx', opts);
% Process the data to convert text to numbers or dates
for i = 1:height(data)
for j = 1:width(data)
currentValue = data{i, j}{1};
if % your logic to determine if it's a date or number
% Convert currentValue and assign back to data{i, j}
end
end
end
3. Use Low-Level File I/O Functions
If the above methods are not flexible enough, consider using lower-level file I/O functions (xlsread, fread, etc.) that give you more control over the reading process. This is more complex but allows you to handle data in a more customized way.
--------------------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  2 Comments
Hassaan
Hassaan on 12 Jan 2024
@Harald Sure. You are welcome.
--------------------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!