
Array not returning any value when passed through a nested function
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am attempting to create a function whereby I can pass two values and it returns a list of values which I can then go on to plot.
I can get this code working by using multiple for loops but my plan was to have one for loop in a nested function and then just call said function as that seems like a neater solution to me. Here is a segment of my code.
function y = testcode
%defining variables
t0 = 0;
xrange = -12.5:0.1:12.5;
u = [];
disp(u)
function createCurve(tValue,uValue)
for x = xrange;
t = tValue;
%wave function
U= 24*((6+8*cosh(4*x - 16*t) + cosh(8*x -128*t))/((6*cosh(2*x -56*t) + cosh(6*x - 72*t))^2));
%allocating the index at the end of u the value of U
uValue(end+1) = U;
end
end
createCurve(t0,u)
disp(u)
end
Currently in the console u is empty at the start as it should be. but after I have called the createCurve function. u is still empty and i'm not sure why. my plan is to be able to call this function 4 times with different u and t values so that I can then plot 4 different curves.
I am using matlab 2017a if that matters?
0 Commenti
Risposte (2)
Stephen23
il 29 Dic 2018
Modificato: Stephen23
il 29 Dic 2018
Inside your nested function you allocate to U, but not to u. MATLAB is case sensitive!
Inside the nested function you allocate U to the variable uValue, but as this variable only exists locally inside the nested function it is unrelated to the main function.
In any case, it is probably not required to use nested functions or loops:
>> X = (-12.5:0.1:12.5).';
>> T = 0:5;
>> M = 24*((6+8*cosh(4*X-16*T)+cosh(8*X-128*T))./((6*cosh(2*X-56*T)+cosh(6*X-72*T)).^2));
>> plot(M)
>> legend({'T=0','T=1','T=2','T=3','T=4','T=5'})

0 Commenti
Steven Lord
il 29 Dic 2018
I suspect you expect this line of code to update the variable that was passed into your nested function as the second input, since the function is declared with the name uValue as the name of its second input.
uValue(end+1) = U;
That is not what will happen. uValue is not a "pointer", "reference", or "handle" to the array u. It is a copy (-on-write) of u.
If you want to modify u inside the nested function, don't pass it into the nested function. Nested functions have access to the workspace in which they are nested, and so it can directly access t0 and modify u.
By the way, you probably want to avoid using both u and U as variables. MATLAB treats them as separate variables, but it's too easy to accidentally hold the Shift key while typing a variable name and use the wrong cased name. For some letters (A and a) the uppercase and lowercase variable names look different enough that this is probably easy to detect. For U and u, they look similar enough that it would be easier to overlook at first glance while you're debugging.
2 Commenti
Stephen23
il 29 Dic 2018
Modificato: Stephen23
il 29 Dic 2018
"is there a way I can pass arrays through the nested function though"
Of course! You can either:
- pass by value using the input and output arguments (just like any other function), or
- access an array in the main function's workspace (in which case simply define it in the main function and refer to it from within the nested function).
Just like any other MATLAB function, input and output arguments are copy on write (i.e. essentially pass by value), so if you change them inside the function then this is not reflected in the variable that you passed to the function. Given that you have a nested function, this becomes a moot point because you can trivially access any variable in the main function's workspace.
Vedere anche
Categorie
Scopri di più su Graphics Performance in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!