How to add values from vertcat to a script

I have script which calls a function and the return values of the function are stored in this way:
M = vertcat(C{:});
C is 1*99 cell and each cell has 4 values.Now I want to store individual value of 1st column to an array 2nd column to another array and so on.So is it possible to do so?

11 commentaires

Stephen23
Stephen23 le 26 Avr 2019
Original question:
@Tipu Sultan: As I wrote in your earlier thread, this would likely be much simpler if you simply returned three outputs from the function. Your approach is making your code more complex.
Tipu Sultan
Tipu Sultan le 26 Avr 2019
Modifié(e) : Tipu Sultan le 26 Avr 2019
How do i get 3 output and then send those to a new script!
Stephen23
Stephen23 le 29 Avr 2019
Modifié(e) : Stephen23 le 29 Avr 2019
"How do i get 3 output..."
How to write functions with mutiple outputs is explained in the documentation:
Get rid of tri and return the three variables that you want, e.g.:
function [z,distance,angle] = fun1(I)
When you call your function you can now use three output arguments.
"... and then send those to a new script!"
Do not write scripts. Write functions, and simply pass data as input/output arguments.
Tipu Sultan
Tipu Sultan le 29 Avr 2019
Modifié(e) : Tipu Sultan le 29 Avr 2019
Thanks @Stephen Codeldick, but when I am using the follwing code:
function [z,distance,angle] = fun1(I)
now the value of M from the code as follows:
M = vertcat(C{:});
has only boolean values only.So now how do I pass the value to my script/function.
Stephen23
Stephen23 le 29 Avr 2019
Modifié(e) : Stephen23 le 29 Avr 2019
@Tipu Sultan: I wrote this:
"When you call your function you can now use three output arguments."
If you define the function to have three outputs then you need to call it with three output arguments, exactly as I told you to do. If you only call it with one output argument, like you are doing now, then clearly you will only get the first output.
How to call functions with multiple output arguments is explained in the introductory tutorials:
and also on the function documentation that I gave you earlier. Please read the documentation.
"If you define the function to have three outputs then you need to call it with three output arguments, exactly as I told you to do. If you only call it with one output argument, like you are doing now, then clearly you will only get the first output."
function [z,distance,angle] = fun1(I)
I am calling the function with three arguments which are z , distance,angle! from above.
Am I doing something wrong or may be I am not getting what you are trying to refer @stephen.
Please help.
Stephen23
Stephen23 le 29 Avr 2019
Modifié(e) : Stephen23 le 29 Avr 2019
"I am calling the function with three arguments which are z , distance,angle! from above."
You told us that you are only getting the z values, i.e. those corresponding to the first output argument. Which means that you have ignored the other two outputs, or have not looked at the variables that you assigned them to.
Remember that the number of output arguments of a function definition is NOT the same thing as how many output arguments it is called with! You seem to be conflating the two.
"Am I doing something wrong..."
Probably, but as you have not shown us how you are calling this function I can only guess that you are calling it with one output argument and have ignored the other two. If you want help, please show how you are calling this function.
I am calling this function as below.
D = 'C:\Users\Tipu_Standard\Desktop\My_project_cropped_images1';
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{:});
My function is below.
function [z,distance,angle] = fun1(I)
B=rgb2gray(I);
A=adapthisteq(B);
% Display the original image
figure,imshow(A);
distance =0;
angle=0;
rmin=7;
rmax=22;
% 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
Stephen23
Stephen23 le 29 Avr 2019
Modifié(e) : Stephen23 le 29 Avr 2019
On this line you call fun1 with only one output argument:
C{k} = fun1(I);
^^^^ ONE output argument!
I already gave you two links that show how to call functions with multiple output arguments. Did you look at them? Or look at madhan ravi's answer below, which also shows the function deal being called with four output arguments? Hint:
[Zvec(k),Dvec(k),Avec(k)] = fun1(I);
Remember that the whole point of having three ouput arguments is to get rid of that complex cell array C, so you will now need to preallocate the output arrays to be the correct classes and sizes:
If any of the outputs are non-scalar then adjust the indexing to suit.
Thanks @stephen.
I changed the code as follows:
D = 'C:\Users\Tipu_Standard\Desktop\My_project_cropped_images1';
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);
Zvec = zeros(1,20);
Dvec = zeros(1,20);
Avec = zeros(1,20);
[Zvec(k),Dvec(k),Avec(k)] = fun1(I);
end
M = vertcat(C{:});
Command window gives me following error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in funCall (line 12)
[Zvec(k),Dvec(k),Avec(k)] = fun1(I);
Is there any problem that I am not able to locate!
Stephen23
Stephen23 le 29 Avr 2019
Modifié(e) : Stephen23 le 29 Avr 2019
Move the preallocation before the loop (just like the preallocation documentation shows):
D = 'C:\Users\Tipu_Standard\Desktop\My_project_cropped_images1';
S = dir(fullfile(D,'Original*.jpg')); % pattern to match filenames.
N = numel(S);
Zvec = zeros(1,N);
Dvec = zeros(1,N);
Avec = zeros(1,N);
for k = 1:N
F = fullfile(D,S(k).name);
I = imread(F);
[Zvec(k),Dvec(k),Avec(k)] = fun1(I);
end
You will also need to adjust the sizes of the output arrays AND the indexing to suit the sizes of the output arguments: if they are non-scalar then you will continue to get errors.
I do not have your data, you will have to check this yourself. After that, adjust the sizes of the output arrays. If the sizes change on between iterations, use cell arrays for the appropriate array/s (in fact if you just want to get your code working, use cell arrays for all three outputs).

Connectez-vous pour commenter.

Réponses (1)

madhan ravi
madhan ravi le 26 Avr 2019
Wanted = num2cell(M,1)
[a,b,c,d]=deal(Wanted{:})

3 commentaires

Tipu Sultan
Tipu Sultan le 26 Avr 2019
Modifié(e) : Tipu Sultan le 28 Avr 2019
This is my first script:
D = 'C:\Users\Tipu_Standard\Desktop\My_project_cropped_images';
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
N = vertcat(C{:});
now the function 'fun1(I)' is like this:
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
And now I want to send the value of 'N' of the first script above to a new script which is as follows:
S = [100 0 0 0 0 0;
0 100 0 0 0 0;
0 0 100 0 0 0;
0 0 0 100 0 0;
0 0 0 0 100 0;
0 0 0 0 0 100];
prev_S = S;
Big_lambda = eye(2);
a=0;
b=0;
c=0;
p=0;
q=0;
s=0;
est_vec=[a ; b; c; p; q; s];
theta = [45 60 90 ] ;
t= [ 10 20 30 ];
r= [ 10 20 30];
Wanted = num2cell(M,1)
[r,b,distance,angle]=deal(Wanted{:})
x = [ r; theta ; t];
time = 40;
for i=1:3
dif_x = [(-cos(theta(i))) (r(i).*sin(theta(i))) (2*a.*t(i)+b);(-sin(theta(i))) (-r(i).*cos(theta(i))) (2*p.*t(i)+q)];
W = dif_x(i)*Big_lambda*dif_x(i)';
M = [t(i)^2 t(i) 1 0 0 0;
0 0 0 t(i)^2 t(i) 1];
y = M * est_vec + dif_x * x(:,i);
K = prev_S*M'/((W + M*prev_S*M'));
S = prev_S;
est_vec_new = est_vec + K*(y-M*est_vec);
cond = abs(est_vec_new - est_vec);
if cond < 0.003
break
end
est_vec = est_vec_new;
S_new = (eye(6) - K*M)*S;
S = S_new;
meas_equa1 = a*time.^2+b*time+c; % measurement euation
meas_equa2 = p*time.^2+q*time+r; % measurement equation
end
Now I want to store the values of 'z,distance,angle' which are derived from the function fun1(I) to the last script array t,r,theta respectively i.e t=z,r=distance,theta=angle. So is it possible to do that?
Any help will be greatly appreciated.
madhan ravi
madhan ravi le 26 Avr 2019
That seems like a different question , I answered to the question you originally asked. It’s better you post it as a separate question.
Tipu Sultan
Tipu Sultan le 28 Avr 2019
@madhan I posted the question but it was closed due to duplicate!
So please help me with the problem.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2015a

Modifié(e) :

le 29 Avr 2019

Community Treasure Hunt

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

Start Hunting!

Translated by