4
$\begingroup$

I was wondering if there an equivalent of the "uigetfile" Matlab function to use it in Mathematica.

To be more precise, I want to read many Excel files and I don't want to type a static path; instead, I want to be able to go each time and search for it, like in the browse window.

Here what I want to do in steps:

  1. Search the directory of my Excel files using the "SystemDialogInput" probably, in order get the path of the directory.

  2. Store the directory in a string.

  3. Load the name of all the Excel files inside the this directory.

  4. Make a loop and read each of the Excel files based on some condition then perform some calculations.

$\endgroup$
1
  • 1
    $\begingroup$ Please describe in more detail how you would like this to work. $\endgroup$
    – Mr.Wizard
    Jan 27, 2015 at 9:11

3 Answers 3

8
$\begingroup$

While I wait for a more complete description of your needs please see:

And try:

FrontEndTokenExecute["FileNameDialog"]

Or:

SystemDialogInput["FileOpen"]
$\endgroup$
8
$\begingroup$

You probably want a workflow similar to this. First locate one or more Excel files:

files = SystemDialogInput["FileOpen", {NotebookDirectory[], {"Excel File" ->{"*.xlsx","*.xls"}}},
WindowTitle -> "Import Excel File"];

Then import the selected files and do something with the imported data (what that something is, we don't know so no point in speculating):

If[files =!= $Canceled && files =!= $Failed,
(* a single file will be a string, several files will be in a list *)
 tmp = Which[
StringQ[files], Import[files],
ListQ[files], Import /@ files,
True, (* some warning message *)
];
 (* then do something with tmp *)
 ]

Edit

If you want to import csv files then change

{"Excel File" -> {"*.xlsx","*.xls"}}

to

{"CSV File" -> {"*.csv"}}

If you would like to choose between Excel and CSV then

{"Excel File" -> {"*.xlsx", "*.xls"}, "CSV File" -> {"*.csv"}}

If you would like both Excel and CSV to be available to select then

{"Excel or CSV File" -> {"*.xlsx", "*.xls", "*.csv"}}

...and so on.

$\endgroup$
4
  • $\begingroup$ I managed to form a string now that has now all the files in repertory for example "basecase", how to keep only csv files? $\endgroup$
    – kevin
    Jan 27, 2015 at 16:25
  • $\begingroup$ @kevin please see my edit $\endgroup$ Jan 27, 2015 at 20:50
  • $\begingroup$ +1 for teaching me Unsame $\endgroup$ Jan 27, 2015 at 23:14
  • $\begingroup$ Thx a lot..it is more clear $\endgroup$
    – kevin
    Jan 28, 2015 at 12:53
1
$\begingroup$

It seems more consistent with the Matlab uigetfile function to say

fnams = FileNames["*.xlsx"|"*.xls"];
selectedFile = ChoiceDialog["Choose File", fnams];
sheet=Import[selectedFile]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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