sorting 2d by double value.
4 views (last 30 days)
Show older comments
Muhammad Awais Ali
on 4 Mar 2022
Commented: Walter Roberson
on 6 Mar 2022
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?
0 Comments
Accepted Answer
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
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
More Answers (1)
Muhammad Awais Ali
on 5 Mar 2022
1 Comment
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
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!