Read input matrix inside a function

4 ビュー (過去 30 日間)
Tom Irvine
Tom Irvine 2011 年 12 月 13 日
I have a numerical array 'drop' setup in my Matlab workspace.
I can read the array into an interactive Matlab script using the prompt:
F = input(' Enter the matrix name: ')
This works fine in a main script.
However, when I place this same command inside a supporting function file called by the main script, I get the following error message upon execution:
??? Error using ==> input Undefined function or variable 'drop'
Please help. Thank you. Tom

採用された回答

Walter Roberson
Walter Roberson 2011 年 12 月 13 日
This is to be expected. Anything you type in in response to an input() call that does not use the 's' option, will be evaluated in the workspace of the function that called input().
You could type in your response to input as
evalin('caller','drop')
  2 件のコメント
Tom Irvine
Tom Irvine 2011 年 12 月 13 日
Is there an easier way?
I am writing interactive Matlab scripts that need to be user-friendly because they will be used by others. I am trying to write a function that will be called by several scripts for efficiency. The function needs to read in a numerical array.
Walter Roberson
Walter Roberson 2011 年 12 月 13 日
It would be better if you were to state your requirements about which variables are to be located and the calling relationship between the function those variables are stored in, compared to the function that needs to do the reading.
In the meantime I would point out that if you were to use the 's' option of input() so that you read the response as a string, you could evalin('caller','who') in order to get the names of the variables known to the caller, and you could compare the input string to those variable names, and have your routine do the evalin('caller', InputString) if there was a match; if there was no match then evaluate the string to an array.

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

その他の回答 (1 件)

Tom Irvine
Tom Irvine 2011 年 12 月 13 日
Oh... this seems to work:
FS = input(' Enter the matrix name: ','s');
F=evalin('caller',FS);

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by