如何让用户从运行时给定列表中选择对象?

时间:2013-04-02 08:53:13

标签: matlab dynamic user-input

我正在编写一个脚本,需要用户选择要工作的子目录。

这里指出我被困住的地方:

curr_dir = dir(pwd);

% Select only subdirectories
dirs = {curr_dir([curr_dir(:).isdir]).name}';
dirs(ismember(dirs,{'.','..'})) = [];

% Ask the user which one to use
dir_selected = 0;
while dir_selected == 0;
    selection = ; % <------------- INSERT HERE MISSING CODE
    if length(selection) == 1
        dir_selected = 1;
    elseif length(selection) > 1
        fprintf('\nPlease enter only one value\n');
    elseif selection > length(dirs)
        fprintf('\nPlease enter a valid value\n');
    else
        fprintf('\nPlease enter a value\n');
    end
end

path_files = dirs(selection);

我需要向用户列出当前的子目录(及其索引),并要求用户输入他想要使用的文件夹的索引。

我见过input()prompt()的示例,但大多数假设消息是静态的。是否可以拥有动态生成的消息/提示?

1 个答案:

答案 0 :(得分:0)

好的,对不起,我找到了解决问题的方法。

我使用以下代码:

fprintf('\nPlease select the desired subdirectory:\n');
for i = 1:length(dirs)
    fprintf('%i: %s\n',i,dirs{i});
end
selection = input('Which is the desired subdirectory?: ');