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 件のコメント

Jan
Jan 2013 年 4 月 8 日
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 日

0 投票

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 件のコメント

Jessica Parry
Jessica Parry 2013 年 4 月 8 日
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 ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by