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

답변 (1개)

Star Strider
Star Strider 2020년 4월 11일
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.
.

이 질문은 마감되었습니다.

제품

태그

질문:

2020년 4월 11일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by