0

I have a question about the following code.

[name file] = uigetfile('*', 'Select an image');
if [name file] ~= 0
    %file found
end

Is there a way of saying that [name file] can be assigned to an array? For example, Filepath[2] = ui.getfile.. (this doesn't work).

When I tried the following code, I only get the filename and not the path. Am I declaring this array wrong?

Filepath{2} = uigetfile({'*.wav;*.mp3;*.aac;*.ogg'}, 'Select a file');
%Filepath{2} = uigetfile('*', 'Select an image');
%noFile = Filepath(0);
%if Filepath[1]~= 0
%if Filepath ~= 0

disp('Loading signal');
disp(Filepath);
%disp(Filepath{0});
disp('Filepath{1}');
disp(Filepath{1});
disp('Filepath{2}');
disp(Filepath{2});

2 Answers 2

2
+50

I think you may be confused about the syntax for multiple "output arguments" of a function. Unlike most programming languages, MATLAB allows functions to have multiple return values; these are called output arguments. When you have a function [a b] = foo(x), it is best to think of this as just the syntax to send the first output to a variable called a and the second output to a variable called b. Even though the syntax [a b] looks like an array, it is best to not think of it that way.

Here's an example of uigetfile:

[filename, pathname] = uigetfile('*.m', 'Select a file');

which gives the filename and path of the selected file, in variables called filename and pathname, respectively.

filename =

foo.m


pathname =

/home/tobin/

But what I don't understand is that this method returns zero if it was canceled or a filled array if it was successfull. So how can I check for zero (file not found)?

Why not try it out?

> [filename, pathname] = uigetfile('*', 'Select a file')

filename =

     0


pathname =

     0

It appears that both outputs are 0 if the uigetfile is cancelled. So you can do this:

[filename, pathname] = uigetfile('*', 'Select a file');
fullname = horzcat(pathname, filename);
if filename == 0
    fprintf('uigetfile was cancelled.\n');
else
    fprintf('user selected "%s".\n', fullname);
end
Sign up to request clarification or add additional context in comments.

5 Comments

And is there a way to globalise these variables [filename, pathname] so I can access them in other methods? I tried declaring them at the top as in other programming languages but that didn't work; also typing global in front of 'm did not work.
There is no need to declare variables in Matlab, nor any way to do so.
If you do global filename pathname, then filename and pathname will become global variables. Unlike in other programming languages, you have to put the global filename pathname line in every function that will access these global variables. See, for example: stackoverflow.com/questions/4911926/…
Now I get it. One last question, could you refer (or write me) me to a good snippet where a custom function is used? I would like to create a method for opening files and then always change the global variables instead of copy pasting the code everytime I wan't to open a file.
To make a custom function, you typically put the code in a dedicated file. For instance, the function myfunction will live in myfunction.m. For an example of how to declare a function in that file, see mathworks.com/help/techdoc/ref/function.html .
2

Array accesses in MATLAB use parentheses for "normal" arrays, or curly brackets for cell arrays. A "normal" array can only hold numeric values, so you'll have to use a cell array -- so try Filepath{2} = uigetfile(...).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.