Matlab functions not interacting with main program.

I can't change an array I have in my main program correctly. When I print the content using "Arrprint" my original [1 2 3 4 5] array is printed, not the one that I've changed to [1 9 3 4 5]. Does it only create a local array inside my functions that is not put back into the original array?
Main program:
A=[1 2 3 4 5];
Arrchange(A);
Arrprint(A);
Arrchange:
function A = Arrchange(A)
A(2)=9;
end
Arrprint:
function A = Arrprint(A)
disp(A);
end

Risposte (1)

Your function works correctly.
You need to return the changed ‘A’ to your workspace (I call it ‘Ac’ here):
A=[1 2 3 4 5];
Ac = Arrchange(A)
Arrprint(Ac)
That will produce the result you want.
.

Questa domanda è chiusa.

Prodotti

Tag

Richiesto:

il 11 Apr 2020

Chiuso:

il 20 Ago 2021

Community Treasure Hunt

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

Start Hunting!

Translated by