sorting 2d by double value.

4 views (last 30 days)
I am getting file address on each iteration
I am trying to save in 2darray using this:
2darr(i,1) = string(file.name);
2darr(i,2) = doubleValue
Later after iteration is completed I’ll sort 2darr by doubleValue and re-iterations using loop and display into plot in doubleValue with Descending Order also the String.
How it could be done?

Accepted Answer

Walter Roberson
Walter Roberson on 4 Mar 2022
Edited: Walter Roberson on 5 Mar 2022
I suggest that you store this as two separate variables, one of which is a string array and the other of which is a numeric vector.
filenames = string({file.name});
num_files = length(filenames);
values = zeros(num_files, 1);
for K = 1 : num_files
%do whatever
values(K) = as_appropriate;
end
[values, idx] = sort(values, 'desc');
filenames = filenames(idx);
cats = categorical(1:num_files, 1:num_files, filenames);
plot(cats, values)
  4 Comments
Muhammad Awais Ali
Muhammad Awais Ali on 6 Mar 2022
Actually file.name is inside loop context, we cannot put obove loop
filenames = string({file.name});
num_files = length(filenames);
values = zeros(num_files, 1);
Walter Roberson
Walter Roberson on 6 Mar 2022
You never set app.score to anything other than 0, so you are not going to get a useful plot.
function btnPushed(app, event)
if ~strcmp(app.lbl.Text,'Database Selected Path:')
if ~isempty(app.QueryFeatures)
app.Listbox.Items = {};
files = dir(fullfile(app.diradd,'*.*'));
files([files.isdir]) = []; %remove . and .. and folders
filenames = fullfile({files.folder}, {files.name});
num_files = length(filenames);
[~, app.extractFname, ~] = fileparts(filenames);
arr = strings(num_files, 2);
for idx = 1 : num_files
filename = filenames{idx};
[~, basename, ~] = fileparts(filename);
basename_str = string(basename);
iter_img = imread(filename);
app.iterGray = im2gray(iter_img);
app.iter_feature = extractLBPFeatures(app.iterGray, "Normalization","L2");
app.compr = pdist2(app.Q_Features, app.iter_feature, "euclidean");
compr_str = string(app.compr);
app.Listbox.Items{end+1} = compr_str + " - " + basename_str + " -> " + idx;
app.Panel.AutoResizeChildren = 'off';
ax = subplot(5, 2, idx, 'XGrid', 'on', "Parent", app.Panel);
imshow(ax, iter_img);
title(ax, basename_str + " = " + compr_str);
arr(i,1) = basename_str;
arr(i,2) = compr_str;
app.score(idx) = 0; %?%?
app.xset = [string(app.iter_feature) basename_str];
app.dataset(i,:) = app.xset;
end
[app.score , idx] = sort(app.score, 'desc');
app.extractFname = app.extractFname(idx);
cats = categorical(1:num_files, 1:num_files, app.extractFname);
plot(cats, app.score)
app.DBtable.Data = app.dataset(:,:);
else
msgbox("Selected Query Image First");
end
else
msgbox("Select source Folder First");
end
end

Sign in to comment.

More Answers (1)

Muhammad Awais Ali
Muhammad Awais Ali on 5 Mar 2022
Hi Thank you for your code. I am believing that the stretegy is proposed is closed to my solution.
function btnPushed(app, event)
if ~strcmp(app.lbl.Text,'Database Selected Path:')
if ~isempty(app.QueryFeatures)
app.Listbox.Items = {};
files = dir(strcat(app.diradd,'\*.*'));
i = 1;
set = [];
arr=zeros((1),(2));
starttoend=1;
for file = files'
if ~strcmp(file.name , '.') && ~strcmp(file.name , '..')
iter_img=imread(fullfile(app.diradd,'\', file.name));
app.iterGray = im2gray(iter_img);
app.iter_feature = extractLBPFeatures(app.iterGray, "Normalization","L2");
app.compr = pdist2(app.Q_Features,app.iter_feature,"euclidean");
app.Listbox.Items = [app.Listbox.Items strcat(string(app.compr)," - ", file.name) , " -> " ,(string(i))];
seprators = {' = '};
app.Panel.AutoResizeChildren = 'off';
subplot(5,2,starttoend,'XGrid','on', "Parent",app.Panel);imshow(iter_img);title(strcat(string(file.name),seprators, string(app.compr)));
starttoend = starttoend + 1;
arr(i,1) = string(file.name);
arr(i,2) = string(app.compr);
app.extractFname = string({file.name});
num_files = length(app.extractFname);
app.score = zeros(num_files, i);
app.xset = [app.iter_feature string(file.name)];
app.dataset(i,:) = app.xset;
i = i + 1;
end
end
[app.score , idx] = sort(app.score, 'desc');
app.extractFname = app.extractFname(idx);
cats = categorical(1:app.score, 1:app.score, app.extractFname);
plot(cats, app.score)
app.DBtable.Data = app.dataset(:,:);
else
msgbox("Selected Query Image First");
end
else
msgbox("Select source Folder First");
end
end
  1 Comment
Walter Roberson
Walter Roberson on 5 Mar 2022
files = dir(strcat(app.diradd,'\*.*'));
files is now a struct array that is a column vector of struct entries
for file = files'
The variable file will become each scalar struct entry in turn
arr(i,1) = string(file.name);
file is a scalar struct, and we know that the name field returned by dir() is a character vector, so file.name will be a single character vector, which you convert to string and store in arr(i,1)
app.extractFname = string({file.name});
file is a scalar struct, and we know that the name field returned by dir() is a character vector, so file.name will be a single character vector, which you wrap in a cell array and pass to string() . string() converts a cell array containing a single character vector into a scalar string() object. So just like arr(i,1) app.extractFname will be a scalar string object containing the name -- they will have the same content
num_files = length(app.extractFname);
scalar string object, so num_files will be assigned 1
app.score = zeros(num_files, i);
All of app.score will be overwritten with the 1 x 1 array of zeros.
Then after the loop you have
[app.score , idx] = sort(app.score, 'desc');
but every time through the loop, you overwrote all of app.score with a scalar 0, so after the loop, app.score is a scalar 0. You sort that 0 in descending order, gettting out a 0 for app.score and a 1 for idx

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!