0
$\begingroup$

I want make a package to add a function to Mathematica that reads all excel files is the working directory that is set after the package is loaded.

BeginPackage["Reader`"]

f::usage="f[] returns data from excel file"

Begin["`Private`"]   

 f[]:=Module[{files},
    files = FileNames["*.csv",Directory[]];
    data = Import[#]& /@ files;];

End[]

EndPackage[]

What is happening

Currently, if I load the package, it immediate reads any excel files in default folder.

<<Reader`

Trouble -- excel files in default folder get read

SetDirectory["somewhere"]
read=f[]
(*Result!*)

What I want

I want to load the package without reading in the excel file, so I can change the working directory before I call f.

<<Reader

Package is loaded but no excel files are read

SetDirectory["somewhere"]
read = f[]
(*Result!*)

Is possible to do what I want or do I need to define the function in a different way?

$\endgroup$
  • 4
    $\begingroup$ Since f is set delayed, after <<Reader` nothing will be imported. Have I missed something? $\endgroup$ – Kuba Dec 5 '15 at 19:35
3
$\begingroup$

I am not sure I understand what you are asking, but try

BeginPackage["Reader`"]

f::usage = "f[] imports data from CSV files in the current directory"

Begin["`Private`"]
f[] :=
  Module[{files},
    files = FileNames["*.csv"];
    Import[#] & /@ files]
End[]

EndPackage[]

The above code worked for me.

Be sure that all the code cells in your package are Input cells with the initialization cell property or Program cells (which get that property by default).

$\endgroup$
  • $\begingroup$ It was very simple, Thank you! $\endgroup$ – Saesun Kim Dec 6 '15 at 0:02

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.