4

I'm importing data from some .asc files in Mathematica using the Import[filename, "Data"] command, and storing it in a table. I've run into a problem where sometimes the .asc files have some empty lines at the end of the file, which result in empty values in the table which causes some problems for me later on.

For instance, when I look at data[[5 ;; (Length[data])]], I get:

{{3446.05, 15.5156}, {3446.18, 14.5156}, ..., {3451.49, 7.51563}, {}, {}, {}, {}}

So my question is: what is the best way to get rid of these empty values? I've looked into either ignoring whitespace in the Import, but haven't found anything that accomplishes that. I've also looked at Delete, but I can't seem to get an expression that matches the empty values.

One way that I can do this is to change Length[data] to 'Length[data]-4`. However, that needs to be potentially changed for each file and I would prefer something that would be a more generalized solution that would work for any file, whether they have whitespace or not.

MattS
  • 717
  • 2
  • 7
  • 22
  • 2
    Strongly related: [Efficient way to remove empty lists from lists?](http://stackoverflow.com/q/6562902/590388) – Alexey Popkov Aug 23 '13 at 00:53
  • @AlexeyPopkov Strongly related, but not entirely the same - I'm ok with there being other empty items within any subsets, and the other question doesn't address the possibility of altering the Import parameters to ignore whitespace. But, it was a very thorough answer and definitely a worthwhile read, thanks for the link. – MattS Aug 27 '13 at 13:32
  • 1
    The `"Data"` `Import` format seems to be undocumented (and it is not listed in `$ImportFormats`), but for `"Table"` format you could use the `"IgnoreEmptyLines"->True` option to get rid of the empty lines. – Alexey Popkov Aug 27 '13 at 13:58

2 Answers2

1

If your imported list were called s, you could use:

s/.{}->Sequence[]

Select[s,Length[#]==2&]

DeleteCases[s,{}]

Partition[Flatten[s],2]
KennyColnago
  • 299
  • 1
  • 2
  • I've only been working with mathematica for a few months and am unfamiliar with some of these functions. I tried reading the documentation on them but I'm still not sure what exactly is going on here. Would you mind explaining how this code accomplishes my task? – MattS Aug 27 '13 at 13:41
  • @MattS Have you tried these methods with your `data`? These solutions do not alter any `Import` parameters, it is just post-processing of imported data. – Alexey Popkov Aug 27 '13 at 14:07
  • @AlexeyPopkov I have not - I have a policy of not implementing any code until I understand what it's doing and how it's doing it. – MattS Aug 27 '13 at 14:08
  • @MattS I think your policy does not apply here: the code is implemented by another person and what you really need is to understand how and why it works. You should start from evaluating these solutions and then go to the Documentation for the functions mentioned (`/.`, `Select`, `DeleteCases` etc.) and work with examples there for understanding how they work. I recommend to start with `ReplaceAll` (`/.`). It is the most general and simple solution. – Alexey Popkov Aug 27 '13 at 14:12
  • 1
    @MattS If you are not happy with the Documentation (I am not happy too) I would recommed to look at the Leonid Shifrin's book ["Mathematica programming: an advanced introduction."](http://www.mathprogramming-intro.org/book/Book.html) It is free and gives short and clear explanation on *Mathematica* basics. – Alexey Popkov Aug 27 '13 at 14:25
  • @KennyColnago I tried this code and it didn't work. I was left with the same issue as before. – MattS Aug 28 '13 at 19:11
  • @MattS - I know some months have passed but I stumbled upon this post just now. As @KennyColnago says, assuming that your data is named as `s`, for the example you gave `{{3446.05, 15.5156}, {3446.18, 14.5156}, {3451.49, 7.51563}, {}, {}, {}, {}}` all the solutions that @KennyColnago gave you work. If this didn't work because your data is somewhat different it would be easier if you gave a different example. Either way, I'm guessing that if you use a `Map[]` together with an example from above it works: e.g. `Map[DeleteCases[#,{}],YourOtherData]` – Sos Nov 13 '14 at 10:26
0

The simplest way is to use the DeleteCases built-in function:

data={{1,2},{},{3,4},{}}

DeleteCases[data,{}]={{1,2},{3,4}}
Art
  • 101
  • 1