Message Boards Message Boards

0
|
6342 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to convert output of Dynamic[] to numerical value?

Posted 9 years ago

I want to use the output of Dynamic[x] in a further computation.

Here's a simple version of what I am trying to accomplish:
Input: a = Dynamic[5]
Output: 5
Input: a + 3
Output: 8

Here's whats I actually get:
Input: a = Dynamic[5]
Output: 5
Input: a + 3
Output: 5' + 3

How can I extract the "5" as a numerical value instead of getting a "5`"?

Thanks.

POSTED BY: Safi Ahmed
2 Replies

There's ways to do what you want. But it's complicated. Here's how you actually want to structure your code.

The first step to using Dynamic is to avoid using it till the last moment. Your code should probably look like this:

a = 5;
b = 3;
m := a + b;

Dynamic[m]

or maybe:

a=5
Dynamic[a+3]

You should note that I used SetDelayed (:=) instead of Set (=) with "m". The difference is important.

POSTED BY: Sean Clarke

This is not how Dynamic work. May be the name Dynamic is confusing. I think WRI should have picked a better name. But all what Dynamic does, is keep the value of expression inside it up-to-date, at the spot in the screen the Dynamic is located.

Lets try new name. Call it keepUpToDate. So you can see that saying keepUpToDate[5] does not make sense, since 5 can not change. It is a number, not a symbol. But when you write keepUpToDate[a] then now everytime a changes elsewhere, because may be your code did a=99, then the spot where keepUpToDate[a] is located, will update to the new value of a which is 99. So you will this spot keep changing as you change a elsewhere in the notebook.

So each time you see Dynamic[someExpression] replace it in your mind by keepUpToDate[someExpression]. Mathematica keeps track of every symbol that changes in the notebook which is tagged as Dynamic, When it sees a dynamic symbol change, it will go back and update everywhere an Dynamic expression that had this symbol in it, on that spot.

POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract