How do I write a function that contain a for loop to go through my filesname to find a match?

I currently collected a set of "name_list" together with a set of "numoflikes_list". Each name has certain num of likes attached to it. (i.e 123456789.jpg, 345 likes; 1432546435.jpg, 23 likes)
And I would like to write a function that go through all my image_name in a folder to find a match with the "name_list" that I have collected and eventually obtaining the num of likes that is associated with my image name.
This is what I have so far, was stuck and not sure how to write the for loop for this function:
function numofLikes=get_num_of_likes(image_name,name_list,numoflikes_list)
for i=1:size(name_list, 1)
name_list{i}

Réponses (1)

It a one-liner; your probably do not want to write a function for it
nol = numoflike_list(strcmp(image_name, name_list));

9 commentaires

i'm thinking a function is needed because they are of different .m files.
I have attached the two different .m files for your reference. testcellv2.m is the file that i collected the name_list and numoflike_list from. whereas cnn_classify.m file is the main file that i will like to load the function to find a match.
can the above solution be used if they are of different .m files?
If you run testcellv2, you should have the corresponding variables in your work space, and you can use them if you run cnn_classify.
so meaning to say i just have to run testcellv2 first, then run cnn_classify next?
is it possible to use the function mtd so that I can just run cnn_classify by loading the testcellv2.mat then afterwhich use the function to find a match to obtain the num of likes?
Yes. Why not? As far as I can see, if this is what you want to compute, then go ahead.
yes. but i am stuck with the for loop and need help with the function.
another can help me with the for loop??
You don't Need a for loop, just use the line I've posted above.
i tried the line u posted but still I still an error message of: Subscripted assignment dimension mismatch.
Error in cnn_classify (line 38) mat(nol,:)=E;
% install and compile MatConvNet (run once)
%untar(['http://www.vlfeat.org/matconvnet/download/' ...
%'matconvnet-1.0-beta12.tar.gz']) ;
cd('C:\Users\karmun cheah\Documents\MATLAB\matconvnet-1.0-beta12');
% run matlab/vl_compilenn
%download a pre?trained CNN from the web (run once)
%urlwrite(...
% 'http://www.vlfeat.org/matconvnet/models/imagenet-vgg-f.mat', ...
% 'imagenet-vgg-f.mat');
% setup MatConvNet
run matlab/vl_setupnn
% load the pre?trained CNN
net = load('imagenet-vgg-f.mat') ;
%load till 7th layer
net.layers=net.layers(1:18);
% load and preprocess an image
for j=1:2
foldername=['Test/fashion' num2str(j) '/'];
file_names=dir(foldername);
% file_names(1:542).name;
for i=1:size(file_names,1)-2
im = imread([foldername file_names(i+2).name]);
im_ = imresize(single(im), net.normalization.imageSize(1:2));
load ('testcellv2.mat');
nol = numoflike_list(strcmp(file_names(i+2), C));
%numofLikes=get_num_of_likes(file_names(i+2).name,C,D);
mat(nol,:)=E;
if (size(im_,3)==1)
for k=1:3
new_img(:,:,k)=im_;
end
im_=new_img;
end
im_ = im_ - net.normalization.averageImage;
% run the CNN
res = vl_simplenn(net, im_);
A=res(19).x;
B=squeeze(A);size(B);
mat(i,:)=B;
end
end
% show the classification result
scores = squeeze(gather(res(end).x)) ;
[bestScore, best] = max(scores) ;
figure(1) ; clf ; imagesc(im) ;
title(sprintf('%s (%d), score %.3f',...
net.classes.description{best}, best, bestScore)) ;
rank = ranksvm(B,E);

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Commenté :

le 4 Nov 2015

Community Treasure Hunt

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

Start Hunting!

Translated by