passing argument between functions

2 Ansichten (letzte 30 Tage)
Elias Moubayed
Elias Moubayed am 27 Jun. 2021
Beantwortet: Walter Roberson am 27 Jun. 2021
i have 2 functions both don't have input arguments they take inputs using input functions
function [t,v] = polystep()
x = input(prompt1);
y = input(prompt2);
z = input(prompt3);
and
function ppp = polysys()
x1 = input(prompt1);
y1= input(prompt2);
z1 = input(prompt3);
i want to call polytep in polysys and give x,y,z the values of x1,y1,z1. is there any way to do this ?

Akzeptierte Antwort

Bhaskar R
Bhaskar R am 27 Jun. 2021
Bearbeitet: Bhaskar R am 27 Jun. 2021
Assign x, y, z output of polystep, that can make your requirement possible
function [t,v, x, y, z] = polystep()
x = input('prompt1');
y = input('prompt2');
z = input('prompt3');
Then
function ppp = polysys()
[~,~, x1, y1, z1] = polystep();
  2 Kommentare
Elias Moubayed
Elias Moubayed am 27 Jun. 2021
i tried it and it didn't work when it reached polystep it asked again for the inputs
Bhaskar R
Bhaskar R am 27 Jun. 2021
Have you removed following statements in polysys function ?
x1 = input(prompt1);
y1= input(prompt2);
z1 = input(prompt3);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 27 Jun. 2021
Yes, it is possible without modifying polystep() or polysys() . However, doing so would be a Bad Idea that would interfere with using input() for other purposes: You would have to replace MATLAB's input() function with a different function that took note of what prompt was passed in and used it to decide whether to make a real input() call (and memorize the results) or else instead pull back one of the recorded values. The code would have to be aware of what the form is of the prompts for the two different functions in order to be able to figure out whether the prompt it was passed should be treated as a new request or else to return one of the previous values that it memorized.
I recommend against this approach in any form.
Instead you should modify your existing functions to only prompt once and then pass the results around as needed, the way that @Bhaskar R shows.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by