Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: link to variable, not variable value

It has been very difficult to use google, MATLAB documentation, I've spent a few hours, and I cannot learn how to

x = 1
y = x
x = 10
y

ans = 10

what happens instead is:

x = 1
y = x
x = 10
y

ans = 1

The value of x is stored into y. But I want to dynamically update the value of y to equal x.

What operation do I use to do this?

Thanks.M

like image 273
Mikkel Rev Avatar asked Feb 10 '13 00:02

Mikkel Rev


People also ask

Does MATLAB pass by reference or value?

Accepted Answer For MATLAB built-in data types, MATLAB always passes arguments 'by value' in the sense that any changes made to input arguments within a function are not visible to the respective variables in the caller workspace.

How do you get the value of a variable in MATLAB?

Description. varValue = getVariable( mdlWks , varName ) returns the value of the variable whose name is varName that exists in the model workspace represented by the Simulink. ModelWorkspace object mdlWks . If the value of the target variable is a handle to a handle object (such as Simulink.

How do you assign a value to a variable in MATLAB?

Description. assignin( ws , var , val ) assigns the value val to the variable var in the workspace ws . For example, assignin('base','x',42) assigns the value 42 to the variable x in the MATLAB® base workspace.


1 Answers

You can also define an implicit handle on x by defining a function on y and referring to it:

x = 1;
y = @(x) x;
y(x) % displays 1
x = 10;
y(x) % displays 10
like image 160
gevang Avatar answered Sep 29 '22 09:09

gevang