How do I return a matrix from a function?
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to write a very simple function that takes as an input an image in .tif format and returns a matrix of doubles where each value corresponds to the value of the pixel in that location of the image.
function A = readMyPicture (fileName)
arguments
fileName % This should be a string, the name of the image you want to read
end
A= double(imread(fileName)); % Converts image to matrix of doubles
end
This seems easy enough, however when I invoke the function (which is defined as a method of a handle class, but anyway I never had problems with other methods such as this one), it tells me the following error (the error appears in red in reality):
A = readMyPicture("example1.tif");
Check for missing argument or incorrect argument data type in call to function'readMyPicture'.
What am I doing wrong? I am very confused.
Thank you!
2 个评论
Dave B
2022-3-22
This code works, could it be that you have a previous version of readMyPicture on the path? Try calling:
which readMyPicture
to make sure you're pointing to the right one?
Or, just in case MATLAB is looking at an old cached copy of readMyPicture (shouldn't be the case, but worth a shot):
clear functions
(or just restart MATLAB)
Demo that what you have works (I cleaned up the spacing, but that was just me being obsessive, what you have is functional):
A = readMyPicture("peppers.png");
imshow(A./255)
function A = readMyPicture(fileName)
arguments
fileName % This should be a string, the name of the image you want to read
end
A = double(imread(fileName)); % Converts image to matrix of doubles
end
回答(2 个)
yanqi liu
2022-3-22
A = readMyPicture("cameraman.tif");
figure; imshow(A, []);
function A = readMyPicture (fileName)
A = [];
if nargin < 1
return
end
try
A= double(imread(fileName)); % Converts image to matrix of doubles
catch
end
end
0 个评论
Image Analyst
2022-3-22
Get rid of the "arguments" and "end" line. They are not needed. Or use @yanqi liu's solution for more robustness.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!