Why won't matlab recall the array ive just created in a function?

Hi,
I've written a function where output P is an array of numbers. Now i want to use P made in the function in another function, ie i want to recall P.
function P = poly_input(y)
% -------------------------------------------------------------------------
% If no value is entered in the beginning
% -------------------------------------------------------------------------
if (nargin ~= 1)
disp(' ');
y = input('Invalid, a single integer value must be entered: ');
while ~isscalar(y) || y ~= floor(y)
disp(' ');
y = input('Invalid, a single integer value must be entered: ');
end
end
% -------------------------------------------------------------------------
% If a decimal is entered in the beginning
% -------------------------------------------------------------------------
if ~isscalar(y) || y ~= floor(y)
while ~isscalar(y) || y ~= floor(y)
disp(' ');
y = input('Invalid, a single integer value must be entered: ');
end
end
disp(' ');
str1 = 'The polynomial will have order of ';
str2 = num2str(y);
disp([str1 str2 '.']);
% -------------------------------------------------------------------------
% Inputting the coefficients
% -------------------------------------------------------------------------
count = 0;
for orders = [0:1:y]
disp(' ')
str3 = 'What do you want the coefficient of x^';
str4 = ' to be; ';
alltogether = [str3 num2str(orders) str4];
count = count + 1;
Z(count) = input(alltogether);
end
P = int2str(Z);
P = str2num(P);
P
However, when i call for P in the command window MATLAB states that no value for P has been entered!?
Thanks

댓글 수: 1

Please lern how to format code properly in the forum. It is a good idea to make it as easy to read as possible. Inserting an empty line after each line of code is not useful, but follow the "? help" link to learn how to use the forum interface.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2013년 4월 8일
편집: Walter Roberson 2013년 4월 8일
The array was created in the workspace of the function, but when you are at the command line, you are using the "base" workspace. http://www.mathworks.com/help/matlab/matlab_prog/base-and-function-workspaces.html
What you need to do in your case is, at the command line, use
P = poly_input();

댓글 수: 1

haha, well now i feel a but stupid- that's a little obvious. must be spending too long on matlab- going delirious! thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by