programming a user response for a prompt

16 visualizaciones (últimos 30 días)
Justin Sung
Justin Sung el 21 de Jun. de 2019
Respondida: Steven Lord el 21 de Jun. de 2019
Hi all,
I was wondering if there was a way to write some matlab syntax that can respond to a prompt for user input. When I used this code:
[filename, directory_name] = uigetfile('*.dat', 'example_file.dat');
fullname = fullfile(directory_name, filename);
data = readcell(fullname);
I get prompted to specify the exact file I want to import into matlab:
(I've blacked out some portions for security reasons)
I wanted to know if there was a way to write some matlab syntax to choose the right file for me, assuming I give the full name of the .dat file I want, in this case, a real file named:
example_file.dat
essentially, I want to declare a newfile name variable within the command window, run my script and have it automatically write data into a file without any user input.
Thanks in advance.

Respuesta aceptada

Steven Lord
Steven Lord el 21 de Jun. de 2019
No, there's no way to have MATLAB select an entry in a system dialog or type into a search box. [Short of using Java's Robot class, that is.]
To do what you describe, I'd split your program into two smaller functions. One is a computational function that accepts the file name, reads it in, and performs all the processing without any user intervention. The second is an interface function that prompts the user to select a file and passes that file name to the computational function. [Alternately you could have your combined function check nargin and prompt if the user didn't specify any input, though that would preclude you from being able to pass any additional inputs in or could require checking if the input specified was a valid file.]
If you've written your combined function the way I think, it may be as simple as pulling the uigetfile call out of that function (to turn it into the computational function.) Then your interface function would be nothing more than a call to uigetfile then a call to the computational function.
In the case where the user needs to select a file, they call the interface function and let it call the computational function.
In the case where you know the file on which you want to operate, call the computational function directly.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by