Equivalent of Fieldnames function for cells

☆樱花仙子☆ 提交于 2019-12-14 00:38:23

问题


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};

I'm trying to retrieve either: a, b, c, d OR the image name without the extension.

I have tried fn = fileparts(e) and fntemp = cell2struct(e,2), but I can't get it working.

Hope this makes sense Thanks


回答1:


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

Use fieldnames(images) to get the names.

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

Use {fieldnames.name} to get just the names.

Containers.map Probably more than you need here, but good to know about. help comtainers.map for more.



来源:https://stackoverflow.com/questions/14130188/equivalent-of-fieldnames-function-for-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!