Using an existing vector in a .M file

Hello! I've made a simple function file and let's say I need to input a vector (50 elements) so that a function gives me the resulting vector for those elements. I have the vector in the workspace but when I try to use it as an input (instead of for example introducing the input like this [1 2 3 4 5... 50]) the vector is not recognised as a valid entry and is as if i hadn't introduced nothing. How could I solve this? Is a problem in the script? I have it as:
P_k='Introduce the 50 values';
P_k=input(P_K);
I'm new into this and I really don't know how can I solve this and hadn't found anything related

8 Comments

Stephen23
Stephen23 on 31 Aug 2018
Edited: Stephen23 on 31 Aug 2018
"I have the vector in the workspace but when I try to use it as an input..."
What does this mean? As "an input" to what exactly? Your example does not generate any error, so it is not clear what problems you are having.
Eduardo Chacin
Eduardo Chacin on 31 Aug 2018
Edited: Stephen23 on 31 Aug 2018
Maybe I'm not explaining myself clearly. I have in the workspace the vector V=[1 2 3 ... 50]. The .M file is called EXERCISE. When I call "EXERCISE" I got the message 'Introduce the 50 values', but when I try to use v (as the input) it shows as if I hadn't introduced nothing (that means... it doesn't recognise the 1 2 3... 50). And I would like to be able to introduce 'v' instead of having to introduce [1 2 3... 50]. Is that possible?
Stephen23
Stephen23 on 31 Aug 2018
Edited: Stephen23 on 31 Aug 2018
"Is that possible?"
That depends on how EXERCISE was written. Is EXERCISE a script or a function?
Note that using input is beloved by beginners, but is really a very ungainly way to interact with code.
Is a function file. How would you do it in a better way?
Stephen23
Stephen23 on 31 Aug 2018
Edited: Stephen23 on 31 Aug 2018
@Eduardo Chacin: change the function to accept input arguments.
For some reason some beginners think that forcing the user to enter everything with input is the best thing since sliced bread. But, as you are finding out now, this just slowly and pointlessly forces the user to interact with the function in just one way: through whatever input prompts they happen throw at the user. This means that it is not possible to call the function efficiently in a loop with different input values, or use it in any optimizing function, or really do anything useful with it at all. In short, input is how some beginners make their functions totally impractical to work with.
The solution is that you should edit that function, get rid of any input commands and replace them with function input arguments, exactly as described in the function help. Then just call the function with the input values that you require. Exactly as all functions should be!
@Stephen Cobeldick Could you show me how please? Better to correct this as I'm a beginner than keeping with it and complicating the interactions.
function C = myFun(A,B)
C = A + B;
end
Then call the function with the required inputs:
myFun(pi,0:10)
@Stephen Cobeldick thank you very much

Sign in to comment.

Answers (1)

M
M on 31 Aug 2018

1 Comment

But that's for storing the input as a vector. I already have the vector and I want to use it as an input.

Sign in to comment.

Categories

Tags

Asked:

on 31 Aug 2018

Commented:

on 31 Aug 2018

Community Treasure Hunt

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

Start Hunting!