made up function not running in matlab, error: unrecognized function or variable. How can I get this fixed?

8 visualizzazioni (ultimi 30 giorni)
Hi i made a function script (attached) to segment an image into 12 donut shapes but when I try to call it, i get the following error:
Unrecognized function or variable 'radialProfile'.
Error in radialprofile (line 20)
[avg_profile, radius] = radialProfile(image1, 12);
Why? How can I fix it? I have been whacking at this for a while appreciate your help!! Thank you.

Risposta accettata

Voss
Voss il 22 Feb 2023
Looks like the first three lines:
image1 = imread('ex.tif');
[avg_profile, radius] = radialProfile(image1, 12);
plot(radius, avg_profile);
should not be in there. It seems like those lines are intended for defining the input of radialProfile, calling radialProfile, and plotting the result; therefore, they should be run outside of radialProfile. (Otherwise you get an infinite recursion.)
Also you should decide whether your function is called radialProfile or radialprofile and use one name everywhere (i.e., the m-file name, the name used in the function definition, and the name used to call the function should all be the same).
  6 Commenti
Voss
Voss il 22 Feb 2023
Modificato: Voss il 22 Feb 2023
"Perhaps does it work for you on one of your ex images?"
Honestly, I didn't run it or even look past the first three lines since that was where the problem was.
But now I've run it (using the image 'peppers.png', which you should have in your MATLAB installation), and I've made some modifications (see attached code, and explanation of modifications below).
"It's been busy for a while."
To increase the speed, you can avoid performing the same calculation numerous times inside a loop. In particular
[x, y] = meshgrid(1:size(image, 2), 1:size(image, 1));
r = sqrt((x - center(1)).^2 + (y - center(2)).^2);
and the subsequent atan2 calls. None of that depends on the segment or bin, so those computations can be done once, before the loops. That sped it up a lot.
Other changes I made include:
  • making mask a logical matrix (true/false) instead of a double-precision matrix of 0s and 1s
  • preventing NaNs from getting into avg_profile (NaNs happen when mask is all zeros) because a NaN anywhere in a column makes the average for that column NaN when you take the mean(_,1) - that's why the plot was all blank (all NaNs)
  • using image throughout instead of image1/image2. (Probably you should use img or something else, though, because image is the name of a built-in function.)
Now I get some result; see if it's consistent with what you'd expect:
imshow('peppers.png'); % show image for reference
figure()
image1 = imread('peppers.png');
[avg_profile, radius] = radialprofile(image1, 12);
plot(radius, avg_profile);

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by