How to store individual return value of a function

I want to store individeual return values to a variable
The code is as follows:
function tri=imageReadFromFolder()
something!
tri=[z,distance,angle];
end
As can be seen it basically reads set of images from a folder and for each image it will have individual 'z,distance,angle' .But after it reads it only shows the result for the last images.
Any help on how to solve this will be highly appreciated.

2 Comments

The question is not clear. What is "something!"? Where does "z,distance,angle" come from? It cannot be seen (especially not "basically"), that a set of images is read here. There is no code to read images, there I cannot guess, why the data of the "last" image are replied only. Most likely you use a loop and write:
for k = 1:numel(images)
img = import_image()
z = fcn(Img)
end
instead of
z(k) = fcn(Img)
% ^^^
Please edit your question and post some working code, which reproduces the problem. If we guess, what the problem is, the answers will be more or less unrelated.
I am giving you the whole code:
function tri=imageReadFromFolder()
D = 'C:\Users\Tipu_Standard\Desktop\testIMAGES';
S = dir(fullfile(D,'Original*.jpg')); % pattern to match filenames.
for i = 1:numel(S)
F = fullfile(D,S(i).name);
I = imread(F);
B = rgb2gray(I);
A=adapthisteq(B);
% Display the original image
figure,imshow(A);
distance =0;
angle=0;
rmin=8;
rmax=30;
% Find all the circles with radius >= 8 and radius <= 30
[centersdark,radiidark] = imfindcircles(A,[rmin rmax],'Objectpolarity','dark','Sensitivity',0.913);
k=numel(radiidark);
viscircles(centersdark,radiidark ,'EdgeColor','b');
if(k==0)
z=0;
else
z=1;
p=2*radiidark;
f=2.1;
w=62;
h=144;
s=2.88;
distance=(f*w*h)/(p*s);
a=(192/2)-centersdark(1);
b=144-centersdark(2);
angle=atan2(a,b)*(180/pi);
end
tri=[z,distance,angle];
end
end

Sign in to comment.

 Accepted Answer

Inputs in Main script help to change the input easily.
Main scripts:
D = 'C:\Users\Tipu_Standard\Desktop\testIMAGES';
S = dir(fullfile(D,'Original*.jpg')); % pattern to match filenames.
for i = 1:numel(S)
F = fullfile(D,S(i).name);
I = imread(F);
% Next line changes required, k{i}, I missed the account the size of function output please check the below @Stephen Comment
k(i)=fun1(I);
end
Here k gives the values of individual images, like k(1),k(2)...
Make the different function file
function tri=fun1(I)
B=rgb2gray(I);
A=adapthisteq(B);
% Display the original image
figure,imshow(A);
distance =0;
angle=0;
rmin=8;
rmax=30;
% Find all the circles with radius >= 8 and radius <= 30
[centersdark,radiidark] = imfindcircles(A,[rmin rmax],'Objectpolarity','dark','Sensitivity',0.913);
k=numel(radiidark);
viscircles(centersdark,radiidark ,'EdgeColor','b');
if(k==0)
z=0;
else
z=1;
p=2*radiidark;
f=2.1;
w=62;
h=144;
s=2.88;
distance=(f*w*h)/(p*s);
a=(192/2)-centersdark(1);
b=144-centersdark(2);
angle=atan2(a,b)*(180/pi);
end
tri=[z,distance,angle];
end

9 Comments

I am getting the follwing error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in funCall (line 6)
k(i)=fun1(I);
KALYAN ACHARJYA did not take into account the size of the function output.
This is easy to fix:
D = 'C:\Users\Tipu_Standard\Desktop\testIMAGES';
S = dir(fullfile(D,'Original*.jpg')); % pattern to match filenames.
N = numel(S);
C = cell(1,N);
for k = 1:N
F = fullfile(D,S(k).name);
I = imread(F);
C{k} = fun1(I);
end
M = vertcat(C{:})
Or you could alter the function to return three outputs and then allocate them to three separate arrays. This might make processing your data easier.
ohh yes, thank you @Stephen
getting error:
Undefined function or variable 'vercat'.
Error in funCall (line 10)
M = vercat(C{:})
vertcat() not vercat()
now the function does not return anything!!
@madhan ravi: thank you madhan ravi, I fixed my comment too.
"now the function does not return anything!!"
Unlikely. The function given in KALYAN ACHARJYA's answer always returns tri. If you actually meant to write that your loop does not iterate even once, then that would likely be caused by no files being found that matching the given pattern. Quite a different problem.
You need to be much more precise in describing what you expect and what you are getting.
For example, what is the value of N ? And the output of this command:
size(M)
yes it worked thanks :)

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2015a

Community Treasure Hunt

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

Start Hunting!