Save Variable in Command Window

19 ビュー (過去 30 日間)
Amanda
Amanda 2013 年 4 月 25 日
I want to ask the user for a path in the command window from running the script, and save the path as a variable in the workspace. But the problem is I don't know how to save the variable.
prompt = 'Enter the path:';
path = input(prompt,'s');
Next, I want the path to be saved as a variable in my workspace. So if I type path in the command window, I will see the string that is saved to path.
Thanks,
Amanda

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 25 日
prompt = 'Enter the path:';
path = input(prompt,'s')
%path is saved as a string. What do you need more?

その他の回答 (2 件)

Kye Taylor
Kye Taylor 2013 年 4 月 25 日
編集済み: Kye Taylor 2013 年 4 月 25 日
You want to use the function path so don't create a new variable called path! Instead try this
prompt = 'Enter the path:';
x = input(prompt,'s');
addpath(x);
Then, typing path should either generate a warning if the string input actually points to nonexisting directory or you'll see the string input at the top of the command window output.
  1 件のコメント
Amanda
Amanda 2013 年 4 月 25 日
I was missing the addpath. Thanks Kye....

サインインしてコメントする。


the cyclist
the cyclist 2013 年 4 月 25 日
Those two lines of code will definitely create a variable in the current workspace (i.e. wherever you call these this code) named path that contains the string entered by the user.
So, I'm not quite sure what the problem is.
Are you sure you are calling this from a script, and not a function? If it is a function, you will need to send the variable as output back to the workspace.
[Also, you might not want to name the variable path, which is the name of a MATLAB function.]

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by