Matlab: Open file with typed filename and/or uigetfile?

In summary, the user is seeking a solution for using a single script that can handle both providing a filename as an input argument and opening a dialog box. They have searched for a solution but have not found anything helpful. A possible solution is to use options in command line programs, such as -g for the GUI dialog and -f for providing a filename. However, the user is unsure how to implement this in MATLAB. They have provided some links for implementing this in C.
  • #1
Beer-monster
296
0
Hi all

1. Homework Statement

I have a function script for doing some analysis of data from a text file and making some plots. My script takes the filename as an input argument - this is very helpful for running the analysis over multiple files and replicating results.

However, I have also found it helpful to be able to browse for the file using the command uigetfile.

So I have two versions of the same analysis script: one that requires a filename and one that opens a dialog box. This quite inefficient and awkward and it would be great if I could use a single script.

The ideal solution would be to have the code branch so that if a filename is provided that file is opened as normal, else a dialog box would appear if no name was provided (i.e. an empty argument.)

The Attempt at a Solution



I've googled around and searched mathworks, but have not found anything that helpful. Would anyone be able to point me in the direction of a solution?

Thanks for your help.BM
 
Physics news on Phys.org
  • #2
Does this help http://www.mathworks.com/matlabcent...-when-running-matlab-in-batch-mode-in-windows

You could try to build something like options in command line programs.
For example use
Code:
script -g
to use the GUI dialog (or make that the default) and use
Code:
script -f /location/to/file.dat
whenever you know where to find the file.

I must say that I don't know how MATLAB handles these kind of things, the above idea, although the link above might give you further leads.

The advantage of the mentioned method is that you can use multiple options at once without bothering about the position.
So
Code:
script -o ExtraOption -f file
does the same as
Code:
script -f file -o ExtraOption

Edit;
added some information for doing this in C, it might give you an idea of how to implement this.
http://courses.cms.caltech.edu/cs11/material/c/mike/misc/cmdline_args.html
http://www.thegeekstuff.com/2013/01/c-argc-argv/
 
Last edited:

1. What is the purpose of the "uigetfile" command in Matlab?

The "uigetfile" command allows the user to interactively select a file from the file system in Matlab. This is useful when the exact file name or location is unknown or when the user wants to select from a list of files.

2. How do I use a typed filename to open a file in Matlab?

To open a file in Matlab using a typed filename, you can use the "fopen" command. This command takes in the filename as a string and opens the file for reading or writing, depending on the specified permissions.

3. Can I use the "uigetfile" command to select multiple files?

Yes, the "uigetfile" command has an optional input parameter for selecting multiple files. By default, this parameter is set to 0, but by changing it to 1, the user can select multiple files in the file dialog.

4. How can I handle errors when using the "uigetfile" command?

You can use the "try-catch" statement in Matlab to handle errors when using the "uigetfile" command. This allows you to catch any potential errors and handle them appropriately, such as displaying an error message to the user.

5. Is it possible to open a specific file type using the "uigetfile" command?

Yes, the "uigetfile" command has an optional input parameter for specifying the file type. By providing a file extension or a list of file extensions, the user will only be able to select files with those extensions in the file dialog.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
708
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
951
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Back
Top