# Creating Variables in Matlab with "k" and Array of Numbers

• MATLAB
• elbarto
In summary, The conversation discusses a problem with creating a variable in MATLAB using an array of numbers and the letter "k." The solution involves using the functions "sprintf" and "eval." The conversation also addresses another problem with working with angles and suggests using the function "atan2" instead of "atan."
elbarto

## Homework Statement

Hi,
Just wondering if someone could please steer me in the right dirrection for the correct syntax in matlab. I am trying to create a variable from an array of numbers and combine it with the letter k, to give me k1=[] k2=[] k3=[] etc. where [] is a 3x3 matracie

## The Attempt at a Solution

This isn't the problem I am working on but the solution to this would be a great help.

a=[1 3];
['k' int2str(a(2))]=[0 1 2;1 2 3;0 1 2]
? Error: An array for multiple LHS assignment cannot contain character string.

Trying to get this

k3 =

0 1 2
1 2 3
0 1 2

Thanks
Elbarto

Use sprintf and eval.

- Warren

Thanks for the reply, I did get the function to work, but I did read on the net that this was a long winded way of going through the process. does MATLAB have any operations to make this simpler?

Just one more question regarding matlab, I am having trouble working with angles. I think sin(alpha) needs to be -1 {or I might just be haveing a really bad day with my maths}. Either way, can someone please explain what I need to do to get pi() instead of 0. I wrote a program the works off inputted co-odinates and it is not operating like it should for horrizontal member.
consider moveing from point (0,0) to (-1,0)

>> dx=-1-0

dx =

-1

>> dy=0- 0

dy =

0

>> alpha=atan(dy/dx)

alpha =

0

>> sin(alpha)

ans =

0

For the rest of my program to work, I need to be able to evaluate sin properly otherwise the output is useless.

This is probably an easy question and the solution might be quite obvious but with my limited experience I haven't managed to think of anything yet.

Thank You
Regards Elbarto

Thanks allot for the reply. Works a treat, just what I was looking for.

Regards Elbarto

## What is the purpose of creating variables in Matlab with "k" and an array of numbers?

The purpose of creating variables in Matlab with "k" and an array of numbers is to store and manipulate data in a convenient way. By using the "k" variable, you can easily access and modify elements within the array, making it easier to perform calculations and analysis.

## How do I create a variable in Matlab with "k" and an array of numbers?

To create a variable in Matlab with "k" and an array of numbers, you can use the following syntax: k = [1, 2, 3, 4, 5]. This will create an array with the values 1, 2, 3, 4, and 5, and assign it to the variable "k".

## Can I use different data types in the array when creating variables with "k" in Matlab?

Yes, you can use different data types in the array when creating variables with "k" in Matlab. For example, you can have an array with both numbers and strings, such as k = [1, "hello", 3, "world", 5].

## How can I access and modify elements within the array when using variables with "k" in Matlab?

To access and modify elements within the array when using variables with "k" in Matlab, you can use indexing. For example, to access the first element in the array, you would use k(1), and to modify it, you would use k(1) = new_value.

## What are some benefits of using variables with "k" and arrays in Matlab?

Using variables with "k" and arrays in Matlab can make your code more efficient and organized. It allows you to easily perform calculations and manipulate data without having to write multiple lines of code. Additionally, it can make your code more readable and easier to debug.

• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
• Programming and Computer Science
Replies
15
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K