Help in MATLAB, plotting a signal

In summary, the user is having trouble displaying a signal in MATLAB and is getting an error related to matrix multiplication. The error is due to using the "*" symbol for matrix multiplication instead of the ".*" operator for element-by-element multiplication. After correcting the expression, the user was able to successfully display the signal.
  • #1
Az83
7
0
I'm pretty new in MATLAB, and I can't seem to get it to display a signal. I have the following:

t=1:1:100;
s= 2*sin(2*pi*.05*t)*5*sin(2*pi*2*t)+.75*randn;
plot(t,s)


But i keeps giving me the following error:

? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> signal at 5
s= 2*sin(2*pi*.05*t)*5*sin(2*pi*2*t)+.75*randn;

Does anyone have any suggestions?
 
Physics news on Phys.org
  • #2
one of the things about MATLAB is that the "*" symbol means matrix multiplication, but has been allowed to be used when either the left or right operand (or both) are scalers (which, in MATLAB is a 1x1 matrix, but they recognize that and assign meaning to a multiplication of that as a scaler against any matrix). but what you can't do is multiply two matrices together (bigger than the 1x1, which is dually interpreted as a scaler), unless the number of columns of the left operand is equal to the number of rows of the right. to do element-by-element multiplication of two identically sized and shaped arrays (i won't call them "matrices" here), you use the ".*" operator. sometimes you'll also need to use the ".^" operator, too, because the normal power operator "^" also has matrix meaning when one of the operands are matrices.
oh, i forgot to correct the expression:

s= (2*sin(2*pi*.05*t)) .* (5*sin(2*pi*2*t)) +.75*randn(size(t));

i think that will work.
 
Last edited:
  • #3
That worked perfectly. Thanks so much for the help and quick reply :-)
 

1. How do I plot a signal in MATLAB?

To plot a signal in MATLAB, you can use the "plot" function. First, define the x and y values of your signal as arrays. Then, use the command "plot(x,y)" to generate a simple line plot of your signal. You can also add labels and customize the appearance of your plot using additional commands.

2. How do I add multiple signals to one plot in MATLAB?

You can plot multiple signals in one plot by using the "hold on" command. This will allow you to add additional lines or plots to your existing figure without overwriting it. You can also use the "legend" command to label each signal and make your plot more informative.

3. Can I change the color and style of my plot in MATLAB?

Yes, you can change the color and style of your plot in MATLAB by using the "plot" function with additional parameters. For example, you can specify the color and line style by adding a third argument to the "plot" command, such as "plot(x,y,'r--')" for a red dashed line. You can also use the "color" and "linestyle" commands to change these properties after the plot has been created.

4. How do I add a title and axis labels to my plot in MATLAB?

To add a title and axis labels to your plot in MATLAB, you can use the "title", "xlabel", and "ylabel" commands. Simply provide a string as the argument for each command, and it will be displayed on your plot accordingly. You can also customize the font, size, and position of these labels using additional parameters.

5. Is it possible to save my plot as an image in MATLAB?

Yes, you can save your plot as an image in MATLAB by using the "saveas" command. This will allow you to save your plot in various file formats, such as PNG, JPEG, and PDF. You can also specify the resolution and size of the image using additional parameters.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
898
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top