Changing data type within a table from type table to any other data type

3 vues (au cours des 30 derniers jours)
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'
>>

Réponse acceptée

Steven Lord
Steven Lord le 23 Mai 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 commentaires
Mark Smith
Mark Smith le 23 Mai 2019
Thank you Steven for your answer. You keyed me to use of the braces to reference the table's individual data which I appreciate.
With your answer, I was able to test changing a column of numbers to string type and it is easy to infer how I can extract a column of char type from my table and change it to datetime.
No guidance seems to be out there that shows how the change is made on one column within the entire table in memory so that when the compution is complete, the original table exists with a modified column 4.
It appears that the only method to obtain the complete table (with col. 4 modified) is to create a new Mx1 column array to hold the modified values and then delete the original column 4, join the new column 4 to the original table and reorder as needed.
Am I wrong or is there a more efficient way to modify an entire column in a table in memory/disk?
Lola Davidson
Lola Davidson le 5 Oct 2021
You can do this in place, no need to delete/join:
>>T.Var4 = datetime(T.Var4);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by