hi everyone
i am intererested in creating a m by 2 matrix where the first column is a datetime and second column is numeric. I thought the below code would work but it is not. how can i fix my code?
Code:
Data = convertvars(Data, 2, @(x) datetime(x, 'InputFormat', 'yyyy-MM-dd'));
FilteredDataRealizedVol = Data(:,["TRADE_DATE","REALIZED_VOLATILITY"]);
MR_Volatility(:,1) = FilteredDataRealizedVol.TRADE_DATE;
MR_Volatility(:,2) = FilteredDataRealizedVol1 + S * (M - FilteredDataRealizedVol1);
Error:
Error using ()
You cannot assign numeric values to a datetime array. Assign to the array's Year, Month, Day, Hour, Minute, or Second
properties, or use datetime(x,'ConvertFrom',...) to create datetime values for assignment.
Error in LinearRegression (line 51)
MR_Volatility(:,2) = FilteredDataRealizedVol1 + S * (M - FilteredDataRealizedVol1);
Thank you

 채택된 답변

Walter Roberson
Walter Roberson 2024년 3월 4일

0 개 추천

You cannot fix your code. It is not possible to create a uniform array in which the first column is datetime and the second column is numeric.
You can create a table(), but not an array.
MR_Volatility(:,2) = FilteredDataRealizedVol1 + S * (M - FilteredDataRealizedVol1);
Note by the way that you are operating on the entire table FilteredDataRealizedVol1 there, not just on the realized volatility

댓글 수: 3

Manny
Manny 2024년 3월 4일
편집: Manny 2024년 3월 4일
thank you both. i created a table. the table is 1 by 2. i am getting an error when i try to assign values to the columns. i think i need to pad the newly created table with data so the sizes are the same. is this correct? if yes, how do i fix this?
Code:
FilteredDataRealizedVol1 = table2array(FilteredDataRealizedVol(:,"REALIZED_VOLATILITY"));
sz = [height(FilteredDataRealizedVol) 2];
varTypes = ["datetime", "double"];
varNames = ["TRADE_DATE","REALIZED_VOLATILITY"];
MRVolatility = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
Error:
Error using ()
Right hand side of an assignment into a table must be another table or a cell array.
Error in LinearRegression (line 55)
MRVolatility(:,1) = FilteredDataRealizedVol.TRADE_DATE;
MRVolatility{:,1} = FilteredDataRealizedVol.TRADE_DATE;
Manny
Manny 2024년 3월 4일
thank you so much!! it works now

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Voss
Voss 2024년 3월 4일

0 개 추천

"creating a m by 2 matrix where the first column is a datetime and second column is numeric"
You cannot mix data types in a matrix (or any array); all elements must be of the same class.
However, you might find that using a table works well.

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

질문:

2024년 3월 4일

댓글:

2024년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by