Pass String as Variable Into Function

Sorry, I posted a previous question that was convoluted and didn't really address my main concern. I'm trying to pass a string which is the variable name of a pre-existing UI object into the 'set' function inside a callback function. For example, set(variable1, ...), but I'm not sure how to convert the string 'variable1' into the literal word variable1 to be used in 'set'. Any help is appreciated! Thanks.
Edit: I'm not fond of using it, but I tried the eval function:
eval(['set(variable' num2str(1) ', "String", "hello")']) ;
Yet it doesn't work (I get the error "Invalid parameter/value pair arguments.")
Second Edit: I found the solution! Just use handles format instead:
eval(['variable' num2str(1) '.String = "hello"'])

1 件のコメント

Mike Wilson
Mike Wilson 2020 年 9 月 21 日
Why bother giving such feedback when obviously you feel so much superior to those who know less than you. Get off your high horse and be nice to people.

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

 採用された回答

Walter Roberson
Walter Roberson 2017 年 12 月 21 日

1 投票

Put your data into the handles structure and then use dynamic fieldnames
varname = sprintf('variable%d', 1);
set( handles.(varname), 'String', 'hello')
Or better yet,
variables = [handles.variable1, handles.variable2, handles.variable3, ....];
which_handle = variables(1);
set(which_handle, 'String', 'hello')

5 件のコメント

Andrew
Andrew 2017 年 12 月 21 日
Thank you for your reply, I'll rewrite my code in that format. If I had been using number/character variables I would have done the matrix/cell solution first, I just wasn't sure how to apply it to handles/UI objects.
Walter Roberson
Walter Roberson 2017 年 12 月 21 日
In the OpenFcn callback for your GUI, you can add code that does the equivalent of
handles.variables = [handles.variable1, handles.variable2, handles.variable3, ....];
guidata(hObject, handles);
And after that you can index into the handles.variables array .
Also: if you give a number of different objects the same Tag, then the handles entry built for them will automatically be a vector of all of the handles (but not in any particular order.)
Andrew
Andrew 2018 年 1 月 2 日
編集済み: Walter Roberson 2018 年 1 月 2 日
Sorry to bother you Mr Roberson, I had one other question. I'm having trouble initializing handles. I wrote my gui by hand without using GUIDE, and I don't have a .fig file (however I added the Opening Callback code as if it was made in GUIDE). Referring to this:
it seems that I'm not launching the gui correctly. I didn't know if there was another way to access handles, or an equally reliable alternative to the solution you suggested.
Walter Roberson
Walter Roberson 2018 年 1 月 3 日
OpenFcn callback is not called automatically; it is a special name that GUIDE knows rather than being a true callback. CreateFcn callback always exist though.
However, you must have a section of your code that is initializing the GUI. You should construct the list of handles as you call uicontrol() or whatever to create the GUI.
variables(1) = uicontrol(...)
variables(2) = uicontrol(...)
and then just make sure you place the variables variable somewhere accessible. http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
Andrew
Andrew 2018 年 1 月 3 日
Ah ok I was able to initialize it that way. It works perfectly, thank you again.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2017 年 12 月 21 日

コメント済み:

2020 年 9 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by