Matlab error how to fix?

2 Ansichten (letzte 30 Tage)
Manny am 4 Mär. 2024
Kommentiert: Manny am 4 Mär. 2024
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?
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 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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 4 Mär. 2024
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 Kommentare
Walter Roberson
Walter Roberson am 4 Mär. 2024
MRVolatility{:,1} = FilteredDataRealizedVol.TRADE_DATE;
Manny am 4 Mär. 2024
thank you so much!! it works now

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Voss am 4 Mär. 2024
"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.


Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by