How do I return a matrix from a function?

2 visualizaciones (últimos 30 días)
Andrea Angella
Andrea Angella el 21 de Mzo. de 2022
Respondida: Image Analyst el 22 de Mzo. de 2022
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 comentarios
Michael Van de Graaff
Michael Van de Graaff el 22 de Mzo. de 2022
You code works for me just fine.
Dave B
Dave B el 22 de Mzo. de 2022
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

Iniciar sesión para comentar.

Respuestas (2)

yanqi liu
yanqi liu el 22 de Mzo. de 2022
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

Image Analyst
Image Analyst el 22 de Mzo. de 2022
Get rid of the "arguments" and "end" line. They are not needed. Or use @yanqi liu's solution for more robustness.

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by