Matlab beginner, making variables

In summary, to make a variable in MATLAB that is dependent on another variable, you can use functions. An anonymous function can be used for a simple formula, while a separate m-file function with a function handle can be used for more complex situations. This allows the variable to automatically update when the dependent variable is changed.
  • #1
Dell
590
0
in MATLAB how do i make a variable which is dependant on another variable, for example

y=2*a+3 for any a,
so that when i change a, y will automatically change

i have only managed to set a value for a, then set y and i get a fixed value for y
 
Physics news on Phys.org
  • #2
Dell said:
in MATLAB how do i make a variable which is dependant on another variable, for example

y=2*a+3 for any a,
so that when i change a, y will automatically change

i have only managed to set a value for a, then set y and i get a fixed value for y

Right, and you can place whatever value or values in 'a' that you like. For instance, if 'a' is a vector containing 7 different values, and the formula is written correctly, then 'y' will contain 7 values, each that function of the corresponding element in 'a':

>> a = [-1 0 1 2 3 4 5]

a =

-1 0 1 2 3 4 5

>> y = 2 * a + 3

y =

1 3 5 7 9 11 13
-Will Dwinnell
http://matlabdatamining.blogspot.com/"
 
Last edited by a moderator:
  • #3
Sounds like you're looking for functions. If you're function is simple, you can use an anonymous function (no separate m-file required) or you can use a separate m-file function with a function handle.

Check out the Mathworks site and search for 'Anonymous Functions' or 'Function Handles' (or search within MATLAB's help).

Is this what you're looking for?

-Kerry
 

FAQ: Matlab beginner, making variables

What is the syntax for creating a variable in Matlab?

The syntax for creating a variable in Matlab is as follows: variableName = value; where variableName is the name you want to give to your variable and value is the value you want to assign to the variable.

How do I assign a value to a variable in Matlab?

To assign a value to a variable in Matlab, you can use the = symbol. For example, x = 5; will assign the value 5 to the variable x.

Can I change the value of a variable in Matlab?

Yes, you can change the value of a variable in Matlab by using the same syntax as creating a variable. For example, x = 5; will change the value of the variable x to 5.

How do I see the value of a variable in Matlab?

To see the value of a variable in Matlab, you can simply type the variable name in the command window and press enter. For example, if you want to see the value of x, you can type x in the command window and press enter.

Can I use special characters in variable names in Matlab?

Yes, you can use special characters in variable names in Matlab, but they must follow certain rules. Variable names can only contain letters, numbers, and underscores, and cannot start with a number. Additionally, Matlab is case-sensitive, so variables with different capitalization are considered different variables.

Similar threads

Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
2
Views
2K
Back
Top