MatLab - de-char a char

You read that right, this is probably the oddest question that I ever asked, but I just want to know if I can de-char a char. I don’t think it’s possible but I thought someone might have a nifty way to get me what I want.
So I have Java and MatLab connected via a socket and I have a global variable in a Simulink model that I want to send to java from an ‘m’ file. Java requests which global variable it wants and MatLab returns it. If I hardcode the global I want everything works wonderfully. But if I parse the name I want from a string it returns a string to java. My code looks like:
Test = regexp(char(remain), ';(?<varname>\w*)', 'names');
out.println(Test(1).varname);
out.println(‘Variable’); - what it does / returns a string
out.println(Variablle); - what I need / return value of sim block
Test(1).varname gives me that name I want but it comes out as: ‘Variable’ which is not what I want. I didn’t know if there was any way I could get rid of the ‘’ around the variable name I need. Thanks.

 采纳的回答

Walter Roberson
Walter Roberson 2012-5-31

1 个投票

7 个评论

I couldn't guess he wanted this...We should list a compilation on how many different ways exist for asking the same painful question.
Not helpful, but if I could "like" your comment, I would haha I see this happen way to many times..
@Lucas: I did not mean to sound rude, but I was rather surprised about Walter's intepretation.
So, do you confirm that you do NOT want to create a variable named as the string?
My interpretation is that Lucas is getting a string in, and needs to return the value of a Simulink variable named in the string. Normally you could use Simulink's get_param() as that takes in strings and returns values, but Lucas made the variable global, so this has become a task of turning a string into the value of a variable in the current workspace named in the variable. You could eval() to do that, but as usual eval() is heavy-handed and a security risk. It would be better if all of the accessible values were to (for example) be put as fields in a single structure, and then dynamic structure field names used... as shown in the FAQ.
... and eval() was mentioned in the FAQ.
@Lucas: eval() is the painful beast. It teaches bad programming habits. That's what I was refering to.
Usually questions involving eval() as potential answer come in very different forms and almost all the time eval() is not the best answer.
Odd, I tested just now and the URL points to the FAQ section I intended, 3 questions further up than the dynamic filename one.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by