karl71
karl71

Reputation: 1092

Matlab function switching return/no return

In Matlab, I've got a function that is called after some special points are found on an image. Depending on how the nearby pixels of that "special point" are, the function must return a structure with many parameters or return nothing.

Is it possible to have a function that by deafult will return something but, in some cases, nothing should be returned? How that "return nothing"'s code should be like? Thank you.

Upvotes: 0

Views: 162

Answers (1)

Bas Swinckels
Bas Swinckels

Reputation: 18488

One common trick in matlab is to use the empty matrix [] to indicate nothing. You could write your function something like (untested code):

function result = analyze(image, special_point)

% your code here

if pixels_are_ok
    result.a = 1;
    result.b = 2;
else
    result = [];
end

If you call this function from your other code, you can use isempty to see if you got a result or not:

result = analyze(image, special_point)

if isempty(result)
    display('did not find anything')
else
    display('found some interesting results')
    display(result)
end

Upvotes: 2

Related Questions