I can't seem to pass a variable from one function to another. I've used functions quite extensively (but I'm still a programming newb), so I'm probably just making a dumb mistake here, but I can't for the life of me find it! This is what I'm doing.
My first function
function [ ToFparam ] = ToF_3D_Viewer( ToFparam, RGBparam, Naviparam, DICOMparam )
ToF_2_DICOM_Coords(ToFparam,Naviparam,DICOMparam);
disp(ToFparam.ROI.XYZ_DICOM); %says it's a non-existent field!
end
My second function
function [ ToFparam ] = ToF_2_DICOM_Coords( ToFparam, Naviparam, DICOMparam )
Naviparam.Endotip_2_Tracker = diag([1,-1,-1,1]);
[m,n,z]=size(ToFparam.ROI.XYZ);
ToFparam.ROI.XYZ_DICOM=reshape(inv(Naviparam.data.Endo_RefHomMat(1:3,1:3))*inv(Naviparam.Endotip_2_Tracker(1:3,1:3))*(reshape(ToFparam.ROI.XYZ,[m*n z]))')',[m n z]);
disp(ToFparam.ROI.XYZ_DICOM) %outputs correctly!
end
I also tried changing my first function to the following, but when I later try to add ToFparam.ROI.XYZ_DICOM to another matrix of the same size, which clearly outputs as a matrix in my ToF_2_DICOM_Coords function, it throws an error, saying I can't add a variable of type "structure"
function [ ToFparam ] = ToF_3D_Viewer( ToFparam, RGBparam, Naviparam, DICOMparam )
ToFparam.ROI.XYZ_DICOM = ToF_2_DICOM_Coords(ToFparam,Naviparam,DICOMparam);
disp(ToFparam.ROI.XYZ_DICOM); %says it's a non-existent field!
end
Thanks in advance
You should ask for a return value in youre function call to ToF_2_DICOM_Coords,
function [ ToFparam ] = ToF_3D_Viewer( ToFparam, RGBparam, Naviparam, DICOMparam )
Tofparam = ToF_2_DICOM_Coords(ToFparam,Naviparam,DICOMparam);
disp(ToFparam.ROI.XYZ_DICOM); %says it's a non-existent field!
end
Related
So, i have been doing this work with matlab and every time i try to get the answer a new problem pops up. The one that repeats the most on the prompt is
??? Input argument "x" is undefined.
The work is about deriving with matlab, i have to derive a function with two diferent derivation methods and i have to get that table. Thanks a lot to everyone that trys to answer im very lost with this subject.
clc,clear;
h=1;
x=1.2;
derivada1=derivada_1(x,h);
derivada2=derivada_2(x,h);
for i=0:1:10
fprintf('%.10f %.10f %.10f\n',h*(10.^(-i)),derivada1,derivada2);
end
The function i have to derive is
function [ fx ] = funcion( x )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
fx=x.^3-3*x.^2-x+3;
end
Method1
function [ dfx1 ] = derivada_1( x,h )
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
fx=feval(funcion,x);
fh2=feval(fx,x+h);
fh3=feval(fx,x-h);
dfx1=(fh2-fh3)/(2*h);
end
Method 2
function [ dfx2 ] = derivada_2( x,h )
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
fx=feval(funcion,x);
fh1=feval(fx,x+2*h);
fh2=feval(fx,x+h);
fh3=feval(fx,x-h);
fh4=feval(fx,x-2*h);
dfx2=(-fh1+8*fh2-8*fh3+fh4)/(12*h);
end
Code
Table of results
You really over-complicated it using feval, it's that simple:
function [ dfx1 ] = derivada_1(x,h )
fh2=funcion(x+h);
fh3=funcion(x-h);
dfx1=(fh2-fh3)/(2*h);
end
The problem with your original code was you didn't use function handles. feval(funcion,x) evaluates funcion and passes the returned values to feval, but funcion requires input arguments. Instead it should be something like feval(#funcion,x) passing a function handle (aka function pointer in other programming languages).
As said in the title, I have a recursive function and I am trying to build a tree data structure out of it to save my results. Every node consists in just one single number. The problem is that when I input the tree to the next call of the function, it seems that only the value of the tree is passed along, not the actual tree. Does anyone know how to pass a reference to the tree instead?
Initial Call:
tree = struct('left', 'empty','right', 'empty','feature','empty');
decisiontree_train(AttributeSet, LabelSet, 50, tree, 'node');
Recursive Function:
function decisiontree_train( data, labels, before_split_purity_percentage, tree, branch )
% a1 is 0, a2 is 1
[ a1_split_data, a2_split_data, a1_split_labels, a2_split_labels, ...
split_feature ] = decisiontree_split( data, labels );
new_tree = struct('left', 'empty','right', 'empty','feature','empty');
if strcmp(branch, 'left')
tree.left = new_tree;
new_tree.feature = split_feature;
elseif strcmp(branch, 'right')
tree.right = new_tree;
new_tree.feature = split_feature;
elseif strcmp(branch, 'node')
tree.feature = split_feature;
new_tree = tree;
end
[ after_split_purity_percentage ] = decisiontree_classcount( a1_split_labels );
if after_split_purity_percentage < 100 && ...
after_split_purity_percentage > before_split_purity_percentage
decisiontree_train(a1_split_data, a1_split_labels, ...
after_split_purity_percentage, new_tree, 'left');
end
[ after_split_purity_percentage ] = decisiontree_classcount( a2_split_labels );
if after_split_purity_percentage < 100 && ...
after_split_purity_percentage > before_split_purity_percentage
decisiontree_train(a2_split_data, a2_split_labels, ...
after_split_purity_percentage, new_tree, 'right');
end
% add variable to workspace
% assignin('base', 'a1_split_data', a1_split_data)
end
Unless you use object oriented matlab, there is no pass by reference. While asking a different question, the answers somehow apply to your case as well. If you are using Matlab 2015b or newer, use Matlab OOP and implement your tree using a handle class. If performance isn't a big concern, do the same.
For the likely reason that both isn't true, you have to work around the issue. Matlab uses copy-on-wrote. Thus changing your functions to take your tree structure as a first input argument and returning the modified it isn't a bad idea. In typical cases only very little data is really copied.
When I execute the following:
function [ x ] = addya( varargin )
x=varargin{1};
t=varargin{1};
if(nargin>1)
for i=2:nargin
t=t+varargin(i);
end;
end
x=t;
The error i am getting is:
addya(1,1) ??? Undefined function or method
'addya' for input arguments of type 'double'.
please suggest changes and errors.
Make sure this function is saved in a file called addya.m.
Moreover, as mentioned by il_raffa there's a typo - inside the loop: you should access varargin using {}.
The following code works for me when saved as addya.m:
function [ x ] = addya( varargin )
x=varargin{1}; %// Why is this needed?
t=varargin{1};
if(nargin>1)
for i=2:nargin
t=t+varargin{i};
end;
end
x=t;
Also, I would suggest to refrain from using i as a loop index due to possible complication with complex numbers.
I have a function that Finds the Critical Points of a function.
function [ cr ] = CritPt(f, var1, var2)
f = sym(f);
fx = diff(f,var1);
fy = diff(f,var2);
[xcr,ycr] = solve(fx,fy);
crpt = [xcr,ycr]
I am supposed to use the function CritPt in the Command Window to define a variable called cp which contains the critical points of f(x,y)=x^2*y+(1-y)^2
When I attempt this I get:
>> cp=CritPt('x^2*y+(1-y)^2','x','y')
crpt =
[ 0, 1]
[ 2^(1/2), 0]
[ -2^(1/2), 0]
Error in CritPt (line 2)
f = sym(f);
Output argument "cr" (and maybe others) not assigned
during call to
"C:\Users\GTAV\Documents\MATLAB\CritPt.m>CritPt".
I have tried many alternatives like syms cp= [cp] = etc etc but there is clearly something I am not understanding. Any help would be greatly appreciated
You're using the function properly in the command window.
The problem is in the function CritPt itself: You need to assign a value to the variable cr. When the function completes, it attempts to return the value of whatever variable you have listed after function, but if that variable is not present you get an error.
If you want to return the value of the variable on the last line, then change your last line to
cr = [xcr,ycr]
Alternatively, you can leave the last line as it is but change the first line so you return crpt:
function [ crpt ] = CritPt(f, var1, var2)
I am new to Matlab and I have the following problem:
A function expects an input parameter of an array of function-handles
handleArray = #( param ) [ ...
#calcSumOfParam ; ... % and so on..
#calcSumOfParam ];
solution = calculateFunc( handleArray );
However, calcSumOfParam() actually needs more/additional input parameters (not only param) and is kind of heavy-structured (only an example here)
function [ sumOfParam ] = calcSumOfParam( param , maxIter , startIter )
sumOfParam = 0;
for iter = startIter : maxIter
sumOfParam = sumOfParam + param( iter );
end
end
Is there a way to do this?
I cannot use anonymous functions as I need to use loops and conditions. Also multiple functions won't work either, as I do not know the size of the handleArray and so the amount of functions (get it through user input).