Ivan
Ivan

Reputation: 7746

Delete row from table is confusing

I create a table like this:

data = table(repmat({'CI'},size(ci.offer)),ci.offer,ci.bid,...
    randi(100,size(ci.offer)),randi(100,size(ci.offer)),...
    cellstr(ci.time),repmat({'quote'},size(ci.offer)),...
    'VariableNames',{'Symbol','AskPrice','BidPrice','AskSize','BidSize',...
    'DateTime','Type'});

Because there is pre market data, I need to delete it. I thought that the statement data(row, :) = []; would do the trick. What is strange is I see the statement being executed by the fprintf statement,

[numlinesData,columns] = size(data)

foundOpen = false;
row = 1;
while(false == foundOpen)
   t = data.DateTime(row, :);
   Bid = data.BidPrice(row);
   Ask = data.AskPrice(row);
   dt = datetime(t);
   hr = hour(dt);
   mint = minute(dt);
   if hr > 14 && mint > 29
       foundOpen = true;
       fprintf('Found open %i', row);
       break;
   else
       fprintf('Deleting row %i\n', row);
       data(row, :) = [];       
   end

   row = row + 1;
end

but when I display the data I see all the rows including premarket data that were in theory deleted?

data(1:150,:)

Am I not understanding data(row, :) = []; ?

Upvotes: 0

Views: 56

Answers (1)

Matthew Gunn
Matthew Gunn

Reputation: 4529

If you want to keep deleting the first row of your table until the row matches some criteria, you should remove the line:

row = row + 1;

Higher performance alternative:

If you're having performance problems, it's almost certainly faster if you do all the deletions at once. For example:

rows_to_delete = [];
...
while(false == foundOpen)
    ...
    if hr > 14 && mint > 29
      foundOpen = true;
      fprintf('Found open %i', row);
      break;
    else
      rows_to_delete(end+1) = row;
    end

    row = row + 1;
 end
 data(rows_to_delete, :) = [];       

Upvotes: 2

Related Questions