单元格的“字段名称"功能的等效项 [英] Equivalent of Fieldnames function for cells
问题描述
正如标题所述,只是想知道是否有一个功能可以用作字段名(http://www.mathworks.co.uk/help/matlab/ref/fieldnames.html),但适用于单元格.
As the title says, just wondering if there is a function that works as fieldnames (http://www.mathworks.co.uk/help/matlab/ref/fieldnames.html) does, but for cells.
所以,如果我有类似的东西:
So if I have something like:
a = imread('redsquare.bmp');
b = imread('bluesquare.bmp');
c = imread('yellowsquare.bmp');
d = imread('greysquare.bmp');
e = {a, b, c, d};
我正在尝试检索:a,b,c,d或不带扩展名的图像名称.
I'm trying to retrieve either: a, b, c, d OR the image name without the extension.
我尝试了fn = fileparts(e)
和fntemp = cell2struct(e,2)
,但是我无法正常工作.
I have tried fn = fileparts(e)
and fntemp = cell2struct(e,2)
, but I can't get it working.
希望这是有道理的 谢谢
Hope this makes sense Thanks
推荐答案
单元格数组不包含任何元信息,例如字段名或文件名.如果要访问该信息,则需要更改数据存储结构.一些选项包括:
The cell array does not include any meta-information, like field name or file name. If you want access to that information you'll need to change your data storage structure. Some options include:
标量结构 当只有一个名称可供参考时非常有用.
Scalar Structure Good for when there is a single name to reference.
images.red = imread('redsquare.bmp');
images.blue = imread('bluesquare.bmp');
使用fieldnames(images)
获取名称.
结构数组 有点一般.允许使用完全通用的名称(包括特殊字符和空格),并在需要时允许其他元数据(例如大小",作者")
Array of structures A little bit more general. Allows completely general names (including special characters and spaces) and additional metadata if you need it (like "size", "author")
images(1).name = 'red';
images(1).im = imread('redsquare.bmp');
images(2).name = 'blue';
images(3).im = imread('bluesquare.bmp');
使用{fieldnames.name}
仅获取名称.
Containers.map
可能比您这里需要的还多,但很高兴知道. help comtainers.map
了解更多.
Containers.map
Probably more than you need here, but good to know about. help comtainers.map
for more.
这篇关于单元格的“字段名称"功能的等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!