How do I get num, txt and raw as a cell array or matrix or table from the “readtable(file_name)” command, without using “[num,txt,raw]=xlsread(file_name)”
5 views (last 30 days)
Show older comments
How do I get num, txt and raw as a cell array/matrix/table from the “readtable(file_name)” command. The alternative command “[num,txt,raw]=xlsread(file_name)” couldn’t be used as it is not recommended by Matlab and requires basic excel (which is decommissioned in our company). So please guide me in this case, Thankyou.
2 Comments
Stephen23
on 5 Sep 2024
Edited: Stephen23
on 5 Sep 2024
"How do I get num, txt and raw as a cell array/matrix/table from the “readtable(file_name)”"
That is not really what READTABLE does. It might be possible, with a large effort.
A much better approach would be to use READCELL, which returns something a bit like the RAW array. From that cell array you could derive the numeric and text parts with a few lines of code.
The best approach would be to rethink your code: do you really have mixed data in every column?
Answers (1)
Gayathri
on 5 Sep 2024
I understand that you want to avoid using "xlsread" function. "readtable" along with other matlab functions, can be used to fulfill the functions of "xlsread". The usage is as shown below.
tbl = readtable('filename.xlsx')
raw = table2cell(tbl)
num = table2array(tbl)
txt = tbl.Properties.VariableNames
If you want "raw" variable to contain headers of the excel file then the following code can be used.
data = table2cell(tbl);
raw = [txt; data]
For more information about "readtable" please refer below link.
I hope the above information is helpful to you.
4 Comments
Gayathri
on 5 Sep 2024
Edited: Gayathri
on 5 Sep 2024
@Arepalli, To make it work on excel files with mixed data I recommend using "readcell" and "readmatrix" function. Please find the below codes. "xlsread" function has been shown for comparison.
[num,txt,raw] = xlsread('myExample.xlsx')
num=readmatrix('myExample.xlsx')
raw=readcell('myExample.xlsx')
txt = raw(cellfun(@ischar, raw) | cellfun(@isstring, raw))
Here "txt" gives all the text values as a nx1 cell array.
For more information about "readmatrix" and "readcell" refer to the below links.
Stephen23
on 5 Sep 2024
Edited: Stephen23
on 5 Sep 2024
"I had considered an excel file with numeric values with text headers."
The OP stated "num returns the numeric data in a matrix. additionally returns the text fields in cell array txt, and both numeric and text data in cell array raw", making it clear that their data includes text. Perhaps they meant something like headers, who knows.
They way that people abused XLSREAD for such data... ugh. Which is why I would strongly advise using more suitable and capable features of tables etc rather than trying to force their code into ugly patterns just to suit a deprecated function that is more than twenty years old.
See Also
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!