GUI Text box

7 ビュー (過去 30 日間)
Zach
Zach 2011 年 5 月 18 日
In my GUI I have a text box where the input looks like x(n)=5*rand(1,1)+x(n); , In my m-file code I get the written statement from the textbox by writing eval(get(handles.textbox,'string')); . Is there a way that the input to my textbox will only be 5*rand(1,1)+x(n); and in my m-file I have x(n)=(something to call the string from textbox). I tired x(n)=eval(get(handles.textbox,'string')); and it does not work.
Let me know if I'm not being clear.
  1 件のコメント
Andy
Andy 2011 年 5 月 18 日
Could you be more clear about what doesn't work? Is there an error? If so, post the message. Does it give unexpected output? If so, post the output and tell us the expected output.

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

採用された回答

Matt Fig
Matt Fig 2011 年 5 月 18 日
S = get(handles.textbox,'string'); % S = 'x(n)=5*rand(1,1)+x(n)'
S = S(6:end)
x(n) = eval(S)
Or, more generally,
S = get(handles.textbox,'string'); % S = 'x(n)=5*rand(1,1)+x(n)'
S = regexprep(S,'(?==)','');
x(n) = eval(S)
This takes away everything up to and including the equals sign.

その他の回答 (1 件)

Andy
Andy 2011 年 5 月 18 日
Well, I suppose you could do something like
eval(['x(n)=' get(...)]);
Or you could use evalin:
doc evalin
But eval is pretty dangerous. You're letting your user execute arbitrary code.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by