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 comentario

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.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Abr. de 2013
Editada: Walter Roberson el 8 de Abr. de 2013
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 comentario

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by