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)”
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2024-9-5
编辑:Stephen23
2024-9-5
"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?
回答(1 个)
Gayathri
2024-9-5
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 个评论
Gayathri
2024-9-5
编辑:Gayathri
2024-9-5
@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
2024-9-5
编辑:Stephen23
2024-9-5
"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!