How can the script decide when to send a variable as a function input or not?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2022-5-21
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Author Block Masks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!