Changing data type within a table from type table to any other data type
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mark Smith
el 23 de Mayo de 2019
Comentada: Lola Davidson
el 5 de Oct. de 2021
I have a table of stock market values of the following types: 4 columns of char data, 7 columns of "numeric" data , and 7 columns of char data. The fourth column of char data I need to change to datetime type for use with the Financial Toolbox. Note that the types of data in the table were taken by looking at the matlab structure s1 created from the data returned by the data source, EODHistoricalData.com.
When trying to use the string and datetime functions (used singly or together) on the 4th column with the table as the input to convert the 4th column to datetime, I get an error stating that the input type is table or struct which cannot be used by string and datetime.
The char labled data values are all enclosed in single quotes when displayed in the Variables window in MatLab from which I infer that they are char vectors. However, when I extract a single value from from the 4th column of the table, the istable function returns true and the ischar and isstring functions return false. This precludes looping through the rows of the table to change the type in each cell of the 4th column.
I must be doing something wrong in accessing the data in the table. I have read a lot of documentation and not seen any answers for the problem as I have stated it. Please show me the error of my ways, thank you.
s1=webread(url); %Reads input from EOCHistoricalData.com and puts in structure
TT1 = struct2table(s1); %Puts data to a table
y=s1(2,1) %Accesses structure associated with the second value/row in the structure returned by webread
xx=TT1(2,4) %Accesses the table's second row's date value which is shown to be of type table
Output:
>> DataDownload_2
y =
struct with fields:
code: 'AA'
name: 'Alcoa Corporation'
exchange_short_name: 'US'
date: '2019-05-20'
MarketCapitalization: 4.5898e+09
open: 24.3300
high: 24.8800
low: 23.8500
close: 23.9900
adjusted_close: 23.9900
volume: 3289200
ema_50d: '26.9530'
ema_200d: '33.5610'
hi_250d: '50.1300'
lo_250d: '23.9900'
avgvol_14d: '3410357.14'
avgvol_50d: '3518797.14'
avgvol_200d: '3795347.60'
xx =
table
date
____________
'2019-05-20'
>>
0 comentarios
Respuesta aceptada
Steven Lord
el 23 de Mayo de 2019
Using parentheses to extract elements from a table returns a table array.
Using curly braces to extract elements from a table returns an array of the class of the data that was stored in the table.
If you know the name of the variable you want to extract from the table use dot indexing to extract that variable from the table.
These represent the first three rows in the summary table in the first section of this documentation page.
T = array2table(magic(5))
smallerTable = T(1:3, 1:3) % table
array = T{1:3, 1:3} % double since the underlying data is double
individualVariable = T.Var4 % double since the underlying data is double
2 comentarios
Lola Davidson
el 5 de Oct. de 2021
You can do this in place, no need to delete/join:
>>T.Var4 = datetime(T.Var4);
Más respuestas (0)
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!