How should I write a script that generates a table containing the sin and cos values for X between 0° and 90°, in 1° increments?

I am a fairly new user to matlab and need some help creating this table. There should be three columns labeled x, sin(x), and cos(x) and 91 rows for the values of x (0 through 90), sin(x), and cos(x). Thanks!

2 Kommentare

What have you tried so far? Are you getting some error messages? What are they?
The folks that answer questions on Matlab Answers are happy to help when a question indicates that you have really tried to solve a problem and need help with specific issues. Asking them to do the work for you is not likely to get you much help. These people are busy, too.
Sorry about that. I realize I was not too clear in what my actual problem was and I apologize for that. So I was originally able to actually create a table, but the issue is that it does not display the entire columns. Instead it does this: x = 0 : 90;
y1 = sin(x);
y2 = cos(x);
t = table(x, y1, y2)
>> test2_5
table =
x y1 y2
_____________ _____________ _____________
[1x91 double] [1x91 double] [1x91 double]
I need every value listed as well as the second two columns to be labeled as sin(x) and cos(x) instead of y1 and y2. Thanks!

Melden Sie sich an, um zu kommentieren.

Antworten (2)

You need to make all columns in your table column vectors not row vectors. Try it this way:
x = (0 : 90)'; % Transpose row vector to form column vector.
y1 = sin(x);
y2 = cos(x);
t = table(x, y1, y2)
Now x is a column vector and consequently y1 and y2 will be also, and since they all have the same number of rows, 91, table() will be happy.

3 Kommentare

By the way, column names need to follow the same rules as any variable, that is, no spaces, can't start with a number, no special characters like ( or ), etc.
If you want column headers that have parentheses, you'll have to use fprintf() and print the header line manually, then loop over all the rows using fprintf() to print out each row one at a time.
can you go into detail on using fprintf to get column headers with more formatted appearance?
for example:
fprintf(' x y \n--------------------\n');

Melden Sie sich an, um zu kommentieren.

Some hints:
x = 0 : 90;
threeColumntable = table(col1Vector, col2Vector, col3Vector);
and look at the functions sind() and cosd(). No for loop is needed - sind() can accept a whole vector of a bunch of numbers, not only one single scalar number.

Kategorien

Gefragt:

am 24 Sep. 2015

Kommentiert:

am 23 Feb. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by