How can I have access to a string variable selected by a popup munu in GUI in Workspace via evalin?

Hello,
I am trying to see the string that is selected in a popup menu of a gui file in workspace. There is a popup menu in the gui that selects among different COM ports. In popup callback function, I want to see the selected COM string in the workspace. But when I use evalin, it makes error. this is the code that I have written in the popup callback function:
str = get(source, 'String');
val = get(source,'Value');
evalin('base','COM = str{val}');
and I want to see COM in workspace.
Thanks in advance,

Answers (1)

It's usually not recommended to toss your function's variables into the base workspace with assignin() (which I assume you meant instead of evalin). Why do you want to do that? See the FAQ: http://matlab.wikia.com/wiki/FAQ?&cb=628#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F I have long 5000-line long scripts using GUIDE and doing all kinds of complicated things and I've never, ever had to copy any of my variables into the base workspace.

3 Comments

Thanks alot. I used assignin() and it seems that there is no error anymore. I need this because I want to use what are selected by popup menus, in other callbacks. Actually I have a gui function with a lot of nested callback functions in it and I thought it would be the easiest way to have access to waht are selected by popups by other callback functions. Why using assignin is not recommended? Is there any better way for a callback to have access to variables of other callbacks? Thank you very much for the FAQ link too.
I believe the most recommended way is to use getappdata() and setappdata(), probably followed by attaching a new field to the handles structure, or declaring it global.
@Samar: EVAL, EVALIN and ASSIGNIN are fine method to drive the programmer into troubles. Avoiding them is a good strategy to avoid problems.

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Asked:

on 28 Apr 2013

Community Treasure Hunt

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

Start Hunting!