3
$\begingroup$

I have 15 datasets in a .xlsx/.format that total to 30 columns of data, and would like to import them all at once. The data are ordered every two columns (columns 1 and 2 are dataset1, columns 3 and 4 are dataset2, etc...)

My first thought was to use Table to import the data into an indexed variable, like so:

data = Table[Import["filepath.xlsx"][[1, All, {i, i + 1}]], {i, 1, 30, 2}]

While the data are successfully imported, I come across an issue where all of columns that are shorter then the longest column are filled up to the length of the longest column. For example, the first column, which is shorter, has extra values that look like {x(1),y(1)},{x(end),y(end)},{,},{,},{,},...

I can still plot the data just fine, but I encounter errors down the line and would like to find a more elegant way to work around this issue that doesn't involve importing 15 different excel sheets.

$\endgroup$
  • $\begingroup$ just work with Select or Cases to strip the blank rows after import. $\endgroup$ – george2079 Jun 13 '17 at 1:26
2
$\begingroup$

Import and store all data that contains numdatasets different data sets:

data = First@Import["yourfile.xlsx"]

Partition into datasets; use DeleteCases to remove empty rows:

datasets = Table[DeleteCases[{"", ""}]@data[[All, i ;; i + 1]], {i, 1, 2 numdatasets, 2}]
$\endgroup$
  • $\begingroup$ I'm not at my computer right now, but I don't quite understand the bounds you're using for Table. Regardless, I understand what you're doing here, so thanks for the help! $\endgroup$ – acquisto2 Jun 13 '17 at 2:39
  • $\begingroup$ @acquisto2 I meant it this way: suppose that you have 15 datasets in your data (so numdatasets = 15); then your iterator in Table must go up to 30, i.e. twice the number of datasets. That's where the 2 numdatasets upper limit for $i$ came from. $\endgroup$ – MarcoB Jun 13 '17 at 12:46
  • $\begingroup$ Yep! Works great, thank you! $\endgroup$ – acquisto2 Jun 13 '17 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.