How can the script decide when to send a variable as a function input or not?
2 views (last 30 days)
Show older comments
Steve Francis
on 21 May 2022
Commented: Steve Francis
on 21 May 2022
This is a question about MATLAB function inputs.
In my case, I am using a function to process a grayscale image. The function manipulates the pixel values in a number of ways. Additionally, I want the function to apply a mask to the image sometimes and sometimes not. To control this, I can apply a true/false flag as one of the function inputs. When a mask is required, I include the mask as an input to the function.
My question is: When I don't want to apply a mask, how do I deal with the mask input? In this case, I don't need to pass a mask to the function and I don't want to make a 'dummy' mask as this is not an elegant solution.
I'm probably missing something fundamental so please feel free to school me. Thanks.
Here's an example script (untested!) to help describe the situation:
% This line calls the function
myNewImage = image_processing_function (0, 'myImage.tif', myMask) % What to put for myMask when no mask required?
% Here is the function
function processedImage = image_processing_function (maskFlag, filename, mask_BW)
image = double(imread (filename));
if maskFlag==1 % check mask flag: 1->apply mask, 0->don't apply mask
image(~mask_BW) = NaN; % applt the mask
end
% do other stuff
processedImage = unit16(image);
end
0 Comments
Accepted Answer
Walter Roberson
on 21 May 2022
No mask flag is needed
function processedImage = image_processing_function (filename, mask_BW)
image = double(imread (filename));
if nargin > 1
image(~mask_BW) = NaN; % apply the mask
Invoke the function with either just a file name, or with a file name and a mask.
More Answers (0)
See Also
Categories
Find more on Author Block Masks 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!