How do I return a matrix from a function?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Dave B
il 22 Mar 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
Risposte (2)
yanqi liu
il 22 Mar 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
0 Commenti
Image Analyst
il 22 Mar 2022
Get rid of the "arguments" and "end" line. They are not needed. Or use @yanqi liu's solution for more robustness.
0 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!