MATLAB Answers

Use string to define path for uigetfile

조회 수: 4(최근 30일)
JB 11 Mar 2018
I am trying to use a string to define the path for uigetfile in matlab GUI. But I can't get it to work. The uigetfile does not use the my string as path. What am I doing wrong?
Here the code I am trying.
[filename, dirname] = uigetfile(fullfile(pwd,'folder1','*.txt'),'select file')

  댓글 수: 0

로그인 to comment.

채택된 답변

Jan 11 Mar 2018
Jan 님이 편집함. 11 Mar 2018
There are two methods:
% Folder is not a cell string and not the name "pwd":
Folder = 'C:\Users\xxx\Documents\DTU\SAXS\folder1';
[filename, dirname] = uigetfile(fullfile(Folder, '*.txt'), 'select file')
This does not work if you want to provide a list of accepted file extensions. Then you can specify the initial folder as 3rd argument:
Folder = 'C:\Users\xxx\Documents\DTU\SAXS\folder1';
[filename, dirname] = uigetfile({'*.txt', 'Text-files (*.txt)'; ...
'*.asc', 'ASCII files (*.asc)'; ...
'*.*', 'All Files (*.*)'}, 'select file', Folder)
% ^^^^^^

  댓글 수: 1

JB 11 Mar 2018
PERFECT. Thanks a lot Jan Simon.

로그인 to comment.

More Answers (1)

Arthur Vidal
Arthur Vidal 11 Mar 2018
Walter Roberson 님이 편집함. 11 Mar 2018
Hi JB, the following code worked well for me. Try it and let me know if work for you.
[filename, dirname] = uigetfile('C:\Users\xxx\Documents\DTU\SAXS\*.txt','select file')
Make sure that the string are not cell variables and also that the path your are sending exist.

  댓글 수: 5

표시 이전 댓글 수: 2
JB 11 Mar 2018
Hi Walter. I tried your code but I does not use the pwd as starting point. When I use:
[filename, dirname] = uigetfile('C:\Users\xxx\Documents\DTU\SAXS\*.txt', 'Select file');
it is working fine.
Jan 11 Mar 2018
Do not use "pwd" as name of a variable, because it is a built-in function. Such "shadowing" causes unexpected behavior frequently.
Walter Roberson
Walter Roberson 11 Mar 2018
The code I suggested works with Mac.

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by