1

I am using the loop below to isolate instances where data was recorded versus those with no data. The data set is very large (varying from 1000-6000 depending on the column) and of mixed data types, so the only practical solution I can think of is using loops.

I can't get the if or while statement to accurately read a blank space. It runs without any errors if I use a for loop, but it never enters the first half of the if-meaning I end up copying, not separating my data. The varying sizes of data make a for loop undesirable.

while (isempty(andover_all{j,1})==1) 
  if andover_all{h,33}==''; 
      current_data{k,4}= formated_date{j};  
      k=k+1;
  else
    current_data{i,1}=formated_date{j};
    current_data{i,2}=andover_data{33}(j);
    i=i+1;
 end
 h=h+1;
end

Andover_all is an array of strings, current_data and andover_data are cell arrays with mixed data types. I have tried using isempty, [], cellfun(@isempty,andover_data), and a function eq.m that allows me to compare cell elements-none of them work. I also don't want to remove empty cells from the data, just skip over them.

Please let me know if you have any ideas

Rob
  • 26,989
  • 16
  • 82
  • 98

1 Answers1

2

The empties are indeed something to get used to. It's like working with inf or NaN; what should things like NaN==NaN or 1/0==inf return? There's special rules for these guys. Simple ones, but you have to get to know them. To make all the special rules for these guys less of a burden, more intuitive and more readable, MATLAB has special functions for them: isinf (to detect inf), isnan (to detect NaN) and isfinite (to detect either inf or NaN).

The empties also have special behavior and special rules that require some getting used to. If you think about it, it all makes sense in the end: What should []==[] return? or 1==''?

Empty, of course. And even if []==false is empty, [] is false when evaluated by an if. Easy right? :)

Unfortunately, there is no equivalent of isinf or isnan to detect empties of a specific type (there is no isemptycell or isemptychar etc.) There is an equivalent of isfinite for empties (which is isempty), which catches either '', {}, or [].

But sometimes it is desirable to have checks for specific empties, as in your case. The empties preserve their class. This means, {} is really a cell, and [] really an array of doubles.

Therefore, to detect empty cells:

>> a = {};
>> iscell(a) && isempty(a)
ans = 
    1

to detect empty strings:

>> a = '';
>> ischar(a) && isempty(a)
ans = 
    1

and to detect empty arrays:

>> a = [];
>> isnumeric(a) && isempty(a)
ans = 
    1
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96