How to extract the numerial values out of readtable output?
139 次查看(过去 30 天)
显示 更早的评论
Good afternoon! My apologies for the many questions related to readtable.
Attached is my data file. I use the below command to read the Excel file into Matlab
T = readtable('AA.xlsx');
I know there will be 1-5 columns (unknow amount) that will be scalar strings. They will all be located towards the rightmost side of the table.
Here is my question. How do I get the numerical columns out of the table T. For example, in the above example, I have a total of 48 columns, but only 47 of them are numerical. What I need is
Dat = T(:,[1:47]);
How do I get Dat (numerical values of the table) in a more automatic way, because the 47 number could vary from file to file.
Many thanks!
0 个评论
采纳的回答
Star Strider
2019-6-4
It is possible, although not without some serious conniptions.
Originally:
AA_Table = readtable('AA.xlsx');
headers = AA_Table.Properties.VariableNames;
then:
AA_C = table2cell(AA_Table); % Convert To Cell Array
NumIdx = cellfun(@isnumeric,AA_C); % Determine Numeric Values
Vrbls = headers(NumIdx(1,:)); % Get Corresponding Headers (If Needed Later)
NrsC = reshape(AA_C(NumIdx), size(AA_C,1), []); % Reshape Vector To Matrix
Nrs = cell2mat(NrsC); % Numeric Array (Finally)
The only works if specific columns are all numeric and other columns are all not numeric. It willl fail otherwise. For what it’s worth, it works here.
4 个评论
Nancy Hammond
2021-11-3
编辑:Nancy Hammond
2021-11-3
Why is this so complicated?
We all use excel products for data?
In all these years, why don't you have a simple procedure for reading date with dates and numeric values?
Star Strider
2021-11-3
For the record, I’m a volunteer here. I have no significant connection with MathWorks, ancd certainly do not make any design decisions.
There have been a number of upgrades to readtable in the last 2½ years since this was posted. It generally imports dates as datetime variables if it recognises the format, otherwise it usually imports them as character arrays that can then be used with 'InputFormat' to convert them to datetime arrays.
Not all Excel files are easy to read because they may not have been created correctly, one example being character variables of numeric values instead of the numeric values themselves. Beyond that, detectImportOptions can be used to specify how fields are read in many situations, however it’s sometimes easier to do the conversion on the immported files instead.
With respect to reading the dates, this file is actually a relevant example —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/223011/AA.xlsx', 'VariableNamingRule','preserve')
T1.DateTime = T1.DATE + timeofday(T1.TIME); % Combine 'DATE' & 'TIME' Into A Single Array
T1.DateTime
Then, ‘DATE’, ‘Year’, ‘Month’, ‘Day’ and ‘TIME’ can be deleted and replaced by ‘DateTime’. They can then be put in a cell array because they are different variable types, and only cell arrays allow that, or the numerical values can be put into a matrix, and the ‘DateTime’ variable into a cell array by itself (or perhaps with ‘EXPOCODE’).
Reading uncomplicated Excel files has become fairly straightforward, actually.
.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!