Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何要求用户从运行时给定列表中选择对象?_Matlab_Dynamic_User Input - Fatal编程技术网

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

Matlab 如何要求用户从运行时给定列表中选择对象?,matlab,dynamic,user-input,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;

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

这里是我被困的地方:

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);
curr_dir=dir(pwd);
%仅选择子目录
dirs={curr_dir([curr_dir(:).isdir]).name}';
dirs(ismember(dirs,{'.','..'}))=[];
%询问用户使用哪一个
所选目录=0;
而dir_selected==0;
选择=;%1.
fprintf('\n请只输入一个值\n');
elseif选择>长度(dirs)
fprintf('\n请输入有效值\n');
其他的
fprintf('\n请输入一个值\n');
结束
结束
路径_文件=目录(选择);
我需要向用户列出当前的子目录(以及它们的索引),并要求用户键入他想要工作的文件夹的索引


我已经看到了
input()
prompt()
的示例,但大多数示例都假设消息是静态的。是否可以动态生成消息/提示?

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

我使用以下代码:

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?: ');