how to pass mat files by reference to the function?

net=load('data/vgg_face.mat');
% --- Executes on button press in upl_img.
function upl_img_Callback(hObject, eventdata, handles)
%passing the image to the network
im_ = single(im) ; % note: 255 range
im_ = imresize(im_, net.meta.normalization.imageSize(1:2));
im_ = bsxfun(@minus,im_,net.meta.normalization.averageImage);
res = vl_simplenn(net, im_);
scores = squeeze(gather(res(end).x));
[bestScore, best] = max(scores);
% printing scores
set(handles.output_score, 'String', net.meta.classes.description{best});
guidata(hObject,handles);
I want the mat file loaded in 'net' to be used inside the function upl_img_callback. How can i do so. I cannot declare net inside the function since everytime this function is called this huge mat file(1 GB) is loaded into the ram again. net is defined in the m file like shown. I don't know how to pass it as argument either since this mat file is defined in the same m file as the function. Please help.

 Accepted Answer

Talha - at what point do you load the net object? Since you are using GUIDE to create your GUI, then you could do this in the OpeningFcn (so it will just occur the once) as
handles.net = load('data/vgg_face.mat');
guidata(hObject,handles);
so that all callbacks will have access to this object. Unfortunately, I don't know what the expense would be in "carrying" around this 1 GB object....

3 Comments

I want this net object to be created when the gui is launched and this net object is being used in 2 pushbutton call backs so it must be accessible to them. I don't want to load this object inside each call back because it takes a lot of time for it to load into the ram everytime callback is called.
Then do what Geoff said to load it into memory the first time, then do what I said to make it available to all your other functions and callbacks without loading it in all over again.
Yes this worked. Exactly what i wanted. Thanks

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!