Sorting Struct elements in Numerical order

I have a program which takes some pictures in a struct and performs some pixel counting in each of the pictures. After running the program when I see the struct, the elements are not arranged in the way I want them to be. Here is the code-
img_folder = ('C:\Users\mm\work\blackpixelcount');
filenames = dir(fullfile(img_folder,'*.JPG'));
Total_image = numel(filenames);
for i= 1:Total_image
f = fullfile(img_folder,filenames(i).name);
im= imread(f);
bw= imbinarize(im);
out=nnz(~bw);
get(i)=out;
end
after running the code I see the struct and its sorted as given below-
x.JPG
Now I want the elements to be sorted in numerical ascending order (1,2,3.....9,10,11,12..).
I might be makigs some mistakes there, since I am biggener in matlab. Help will be really appreciated.

 采纳的回答

9 个评论

Tried with the command 'natsortfiles({filenames.name});'
did not work.
@Md Farhad Mokter: did you first download the function from the link that KSSV gave you, and then unzip the zip file onto your MATLAB path (e.g. into the current directory) ?
@Md Farhad Mokter: "Did not work" is too lean to be clear. Please explain what you observe.
Md Farhad Mokter's "Answer" moved here:
I used the given command at the end of program,
b= natsortfiles({filenames(:).name};
I got the names in b variable sorted in natural order. Now I want to sort the elements in "filenames" before the 'for loop' starts. I tried this-
img_folder = ('C:\Users\mm\work\blackpixelcount');
filenames = dir(fullfile(img_folder,'*.JPG'));
b= natsortfiles(filenames);
Total_image = numel(b);
for i= 1:Total_image
f = fullfile(img_folder,b(i).name);
im= imread(f);
bw= imbinarize(im);
out=nnz(~bw);
get(i)=out;
end
and I got error : "Error using natsortfiles (line 123)
First input <X> must be a cell array."
"I used the given command at the end of program, "
b= natsortfiles({filenames(:).name};
But this is what you actually have in your code:
b= natsortfiles(filenames);
Note that the output of natsortfiles is a cell array of character vectors, so your line
fullfile(img_folder,b(i).name);
will also throw an error (because you try to access a cell array as if it was a structure).
Also note that the natsortfiles documentation includes examples of how to use it. I recommend that you read its documentation, so you can avoid these basic errors.
@Stephen Codeldick Sir, I used that command at the end of program, it worked. But the 'filenames' variable remained same as before. So, I tried to put it before the loop and thats what I gave here.
Even. putting the command below command returns error: Dot indexing is not supported for variables of this type.
b= natsortfiles({filenames(:).name};
"But the 'filenames' variable remained same as before"
Sure. You only define it once (on the second line of your code), nowhere else in your code do you make any changes to filenames. You assigned the output of natsortfiles to variable b, so b contains the sorted filenames.
Follow the examples in the natsortfiles documentation.
@Md Farhad Mokter: Use this:
img_folder = ('C:\Users\mm\work\blackpixelcount');
filelist = dir(fullfile(img_folder,'*.JPG'));
filenames = {filelist.name}; % added
b = natsortfiles(filenames);
Total_image = numel(b);
getList = zeros(1, Total_image); % Pre-allocate, avoid "get" as name
for i = 1:Total_image
f = fullfile(img_folder, b{i}); % not b(i).name
im = imread(f);
bw = imbinarize(im);
getList(i) = nnz(~bw);
end
get is an important Matlab command, so avoid using it as name of a variable.

请先登录,再进行评论。

更多回答(1 个)

it worked !!
After reading the documentation part once again, I edited the code in line 3 and line 4.
img_folder = ('C:\Users\mm\work\blackpixelcount');
filenames = dir(fullfile(img_folder,'*.JPG'));
[~,ndx] = natsortfiles({filenames.name});
filenames = filenames(ndx);
Total_image = numel(filenames);
for i= 1:Total_image
f = fullfile(img_folder,filenames(i).name);
im= imread(f);
bw= imbinarize(im);
out=nnz(~bw);
get(i)=out;
end
Now It is all good. My apologies for mistakes and thank you so much.
@Jan, Thank you for the help. I will now give it a try.

类别

帮助中心File Exchange 中查找有关 Cell Arrays 的更多信息

产品

版本

R2018b

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by