How do I test a variable to see if it is empty?
14 views (last 30 days)
Show older comments
Eric Metzger
on 17 Oct 2021
Commented: Eric Metzger
on 17 Oct 2021
I have a data file that I read in using readtable. It reads the file fine but is has a number columns which come up empty and readtable will just make it a simple empty cell. This is not a hugh deal but I need to find a way to test if the cell is actually empty and if so, to put a value in its place which is based on what was done previously. I have done this before and have a fast process to make this happen, just can't seem to figure out how to deal with the empty value cell.
Is there a simple test or check I an use in MatLab that will return a value that can be used in an "if" statement?
Here is a small sample of the readtable results. It comes from a very large .xlsx file so it is not feasable to alter the value in the source file
'5628' '9/30/2021 7:28:56 AM' '' '38' '' '' ''
'5629' '9/30/2021 7:29:02 AM' '64.652' '' '' '' ''
'5630' '9/30/2021 7:29:11 AM' '' '39' '' '' ''
'5631' '9/30/2021 7:29:27 AM' '' '40' '' '' ''
'5632' '9/30/2021 7:29:42 AM' '' '41' '' '' ''
'5633' '9/30/2021 7:29:57 AM' '' '42' '' '' ''
'5634' '9/30/2021 7:30:02 AM' '64.481' '' '' '' ''
'5635' '9/30/2021 7:30:12 AM' '' '43' '' '' ''
'5636' '9/30/2021 7:30:28 AM' '' '44' '' '' ''
'5637' '9/30/2021 7:30:46 AM' '' '45' '' '' ''
'5638' '9/30/2021 7:31:02 AM' '64.31' '' '' '' ''
'5639' '9/30/2021 7:31:07 AM' '' '46' '' '' ''
The first colume is the event line or number or recorded events
Second column is obvious
The thrid and fourth columns are the tricky part. These are the values recorded at the event time. Note the " is in place of a NaN or zero. Readtable puts this in an empty cell which I need a way to quickly test if it is empty and then do something based on previous information ingested.
Any thoughts?
0 Comments
Accepted Answer
Walter Roberson
on 17 Oct 2021
Use detectImportOptions() on the file, and use setvartype() to force the columns to be double, and then readtable.
When you do that, empty entries will come up as NaN. You can then use fillmissing() which has a specific option to use the most recent non-nan value.
More Answers (0)
See Also
Categories
Find more on Spreadsheets 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!