Error while querying timetable
Show older comments
Hello All
I receive the error below when trying to run the code:
Error using table (line 233)
All table variables must have the same number of rows.
Error in Lookingfordrops (line 35)
dropInfo = table(repmat(currentDate, numel(row), 1), columnsToCheck(col)', row, difference(sub2ind(size(difference), row, col)), ...
The main idea is to iteriate through the days in my table and look for drops in value equal or more than 2400 day after day for a specific cell. In my code I am also focusing on only one column at a time. So in the code below, for example, column 3.
Can anyone advise on what's causing the issue? The code is still a work in progress as I haven't figured out how to store the 'drops' in a new table yet. I'm also attaching the table newTable.
columnsToCheck = (3);
dropsTable = timetable();
uniqueDates = unique(newTable.Time);
% Loop through each row in newTable
for i = 2:length(uniqueDates)
currentDate = uniqueDates(i);
previousDate = uniqueDates(i - 1);
% Extract data for the current and previous dates
currentData = newTable(newTable.Time == currentDate, columnsToCheck);
previousData = newTable(newTable.Time == previousDate, columnsToCheck);
% Convert timetable data to table data
currentTable = table2array(currentData);
previousTable = table2array(previousData);
% Calculate the difference between the dates
difference = currentTable - previousTable;
% Drops
drops = abs(difference) > 2400;
% Find rows with drops
[row, col] = find(drops);
% Create a table with information about drops
dropInfo = table(repmat(currentDate, numel(row), 1), columnsToCheck(col)', row, difference(sub2ind(size(difference), row, col)), ...
'VariableNames', {'Date', 'Column', 'Row', 'DropValue'});
% Append the drop information to the dropsTable
dropsTable = [dropsTable; dropInfo];
end
Happy New Year and thanks for your time
2 Comments
Dyuman Joshi
on 1 Jan 2024
Edited: Dyuman Joshi
on 1 Jan 2024
For making a table, every data set needs to have the same amount of rows, which is not the case.
To solve the issue, you will have to make sure that each array (of whatever data type) used to make the table has same amount of rows.
Note - The error occurs while defining the table dropInfo.
OcDrive
on 1 Jan 2024
Accepted Answer
More Answers (1)
Sonesson
on 1 Jan 2024
0 votes
Hello OcDrive,
There are a couple of issues ahead of you as far as I can see.
Your first problem, which causes the error, is mismatching dimensions when you are creating the table dropInfo. For your first iteration everything in dropinfo is a 0x1 vector except columnsToCheck(col)' which is 1x0. To fix this simply remove the " ' ", transposing it (or add another if you are feeling adventurous).
Second issue is that the table() function can not have entry which shares the name of a variable, thus "Row" (table() is not case sensitive in this regard) will not be a valid name, either change the table entry name, or the variable name.
Third issue is when appending the data into the dropsTable. You predefine dropsTable as a 0x0 table and try to append a 0x4 table which will again give mismatching dimensions. To fix this, simply define it as a 0x4 from the get go, or add an if statement to handle the first iteration specially.
Happy new Year!
1 Comment
OcDrive
on 1 Jan 2024
Categories
Find more on Data Type Identification 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!
