Matlab:使用MatlabGUI计算文件夹中的图像

Matlab:使用MatlabGUI计算文件夹中的图像,matlab,Matlab,我想使用Matlab guide 2015b中创建的GUI计算文件夹中的图像数量。 我编写了以下代码: Id = 3 (actually the value of id will be given by user at run time) path =strcat ( ' c:\user\Desktop\New\TrainData\',Id) path=strcat (path,'\') d=dir (path) n=length (d) 它显示了dir不能用于单元格输入的

我想使用Matlab guide 2015b中创建的GUI计算文件夹中的图像数量。 我编写了以下代码:

Id = 3 (actually the value of id will be given by user at          run time)
path =strcat ( ' c:\user\Desktop\New\TrainData\',Id)
path=strcat  (path,'\')
d=dir (path)
n=length (d)
它显示了
dir
不能用于单元格输入的错误。当我使用命令提示符时,此代码正在运行。 只有当我想通过GUI使用它时,它才会显示错误。起初我认为这是一个关于路径的问题。 所以我显示了路径,但它给出了完美的结果。
我很困惑。请在Matlab中提供一些解决方案,而不是在strcat中提供。您应该使用
fullfile

path = fullfile('c:\user\Desktop\New\TrainData',num2str(Id))
注意dir,dir还会列出子文件夹,因此请检查是否只考虑图像文件:

d    = dir(path);
name = d(~[d.isdir]).name 

您应该使用
fullfile
,而不是
strcat

path = fullfile('c:\user\Desktop\New\TrainData',num2str(Id))
注意dir,dir还会列出子文件夹,因此请检查是否只考虑图像文件:

d    = dir(path);
name = d(~[d.isdir]).name 

很可能是您从
inputdlg
或其他东西获取
Id
变量。它是作为字符串的单元格数组而不是字符串读入的。您可以使用
iscell
进行检查:

iscell(Id)
%   1
在点击
dir
命令之前,您不会看到任何问题,因为
strcat
能够很好地处理这个问题,但也会生成一个字符串单元格数组

out = strcat('123', {'4'});
class(out)
%   cell
如果仔细阅读错误消息,错误会明确指出
dir
的输入是单元格而不是字符串。解决此问题的方法是首先检查
Id
是否为单元格数组,必要时转换为字符串

Id = inputdlg('Enter an ID');

% Convert to a string if Id is a cell array
if iscell(Id)
    Id = Id{1};
end

% Get a listing of all files/directories
d = dir(fullfile(folder, num2str(I)));

% Get number of files
nFiles = sum(~[d.isdir]);

此外,您不希望尝试将数字与字符串(
strcat('abc',1)
)连接起来,因为这会将数字转换为ASCII码。相反,您需要使用如上所示的
num2str

很可能是您从
inputdlg
或其他东西中获得了
Id
变量。它是作为字符串的单元格数组而不是字符串读入的。您可以使用
iscell
进行检查:

iscell(Id)
%   1
在点击
dir
命令之前,您不会看到任何问题,因为
strcat
能够很好地处理这个问题,但也会生成一个字符串单元格数组

out = strcat('123', {'4'});
class(out)
%   cell
如果仔细阅读错误消息,错误会明确指出
dir
的输入是单元格而不是字符串。解决此问题的方法是首先检查
Id
是否为单元格数组,必要时转换为字符串

Id = inputdlg('Enter an ID');

% Convert to a string if Id is a cell array
if iscell(Id)
    Id = Id{1};
end

% Get a listing of all files/directories
d = dir(fullfile(folder, num2str(I)));

% Get number of files
nFiles = sum(~[d.isdir]);

此外,您不希望尝试将数字与字符串(
strcat('abc',1)
)连接起来,因为这会将数字转换为ASCII码。相反,您需要使用如上所示的
num2str

什么时候
'edit'
uicontrol
'String'
属性返回单元格数组?@excaza它可以容纳单元格数组,但您是对的,我实际上在考虑
inputdlg
。我会更新。
'edit'
uicontrol
'String'
属性何时会返回单元格数组?@excaza它可以容纳单元格数组,但你是对的,我实际上想的是
inputdlg
。我会更新的。