1
votes

I'm quite new to MATLAB. I've defined a function inside a .m file, and I want to use that function inside another .m file. I want to run the content of that last .m file from the command window.

I have function [feature]=hog(image). How do I initialize it in another .m script?

1

1 Answers

0
votes

You are correctly separating the function definition and the function call. The definition is inside the first .m file you described. It is important that there is only one function per file (excluding things like local, anonymous and nested functions) and that the file has the same name as the function. In your case, the file containing the function hog has to be called hog.m.

Inside the script, you run or call functions. Suppose you have an image I, you can call your function hog by writing e.g. myFeatures = hog(I);. Now you can work with the new variable myFeatures. Note that the script has to be in the same folder as the function (or the path to the function has to be added by addpath('/path/to/function/folder'). To call the script from the command window, simply type the name of the script.

Example structure:

Inside hog.m (function definition):

function [feature] = hog(image)
    % The code of HOG
end

Inside the script (e.g. runHogDetection.m), (function call)

% Read some image
I = imread('cameraman.tif');

% Get HOG features
myFeatures = hog(I);

% Do whatever else you need

And in the command window, you simply call

runHogDetection