Fail to create a new variable in the Timetable
Show older comments
I have a 3 column timetable and want to create the fourth column with values assigned from the third column with a pre-specified step. Running this code, gives me an error of 'To assign to or create a variable in a table, the number of rows must match the height of the table.'
ShiftDates = ExpirationDates1.'
DateNumber = datenum(ShiftDates)
for n = DateNumber
if datenum(Data.Date) >= n & datenum(Data.Date)< (n+91)
Data.Rate = Data{datestr(n),3}
else Data.Rate = 3.686
end
end
Accepted Answer
More Answers (1)
KL
on 22 Nov 2017
if Data is your timetable, you cannot just say
Data.Rate = Data{datestr(n),3} or Data.Rate = 3.686
Pre-allocate the new column first, like
Data.Rate = cell(height(Data),1);
and then use indexing inside your for-loop,
Data.Rate(k) = Data{datestr(n(k)),3}
additional tip: Use the for loop to index through another variable. For example,
for k=1:numel(DateNumber)
if DateNumber(k)...
2 Comments
Angelina
on 23 Nov 2017
KL
on 24 Nov 2017
You probably do all this without any loop, I do not have access to your variables so I didn't test the code. But something like the following should work.
Data.Rate = repmat({'NA'},height(Data),1);
indx_a = datenum(Data.Date) > 737044;
indx_b = datenum(Data.Date) < 733117;
indx_c = ismember(datenum(Data.Date),733117:91:737044);
Data.Rate(indx_a) = -0.329;
Data.Rate(indx_b) = 3.686;
Data.Rate(indx_c) = Data{datestr(n(1)),3};
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!