Clear Filters
Clear Filters

How to use the "subplot" and "arrayfun" command correctly?

2 views (last 30 days)
Hi, I'm wondering if someone can assist me with debugging this Error I received when I ran this .m file program. The code is below:
Ts = [0.05 0.01 0.001]; % Define sampling time, Ts = 0.05 seconds.
TT = 1; % Define total time, TT = 1 second.
f = 2; % Define frequency, f = 2 Hz.
t = 0:Ts:1; % Generate the time vector.
S = arrayfun(t, Ts); % Applies the function, t, to the elements
% of S, one element at a time.
% arrayfun concatenates the outputs from t into the output array S, so that
% for the ith element of Ts, S(i)=t(Ts(i)).
x = sin(2*pi*f*t); % Generate the desired 2 Hz sine wave.
plot (S,x,'.k') % Plot the sine wave as discrete points.
xlabel 'Time (s)' % and label the (x) and (y) axes.
ylabel 'x(t)'
I'm receiving 1 warning in Line 4 that states: "Variable 'Ts' might be set by a nonscalar operator." And 1 Error that states:
"Error using arrayfun"
First input must be a function handle.
Does anyone know how I can possibly fix this Error?
All I'm trying to do is create a subplot of 3 different plots of a discrete 2-Hz sine wave (one for each sampling time [0.05, 0.01, 0.001]) on 1 single plot. I need to create the subplot so I plot with lines connecting the points, and on the same plot, the individual points should be superimposed on the line curves. I need to plot the sine waves using the 3 sample intervals separately but I need to use 'subplot' to keep the 3 plots together in 1 Figure.
I hope that doesn't sound too confusing. If you need some clarification, please let me know. I'm not sure how to use the 'subplot' command correctly. Thanks!
  2 Comments
Rik
Rik on 1 Feb 2018
Have you read the documentation? That is always a good start. Here, the error is also illuminating: t must be a function handle (or an anonymous function), but it isn't. I can't cell what function you would want to use here, but this isn't a function.
Michael
Michael on 1 Feb 2018
Yeah, I'm reading the documentation right now. I just need 3 plots in 1 Figure: 1 plot for each sampling time.
And I'm not sure how I can do that without maybe declaring each sampling time separately - but then that lengthens my code considerably because then I have to create 3 different time vectors for the 3 different sine waves.
I'm just trying to get the sine wave generated and the 3 plots made into a single plot Figure using a "Ts" array containing the 3 different sampling times (one sampling time per plot).

Sign in to comment.

Answers (1)

Rik
Rik on 2 Feb 2018
Do you mean something like this? (note that I changed the input to plot, as well as the creation of t)
Ts = [0.05 0.01 0.001]; % Define sampling time, Ts = 0.05 seconds.
TT = 1; % Define total time, TT = 1 second.
f = 2; % Define frequency, f = 2 Hz.
for Ts_i=1:numel(Ts)
t = 0:Ts(Ts_i):TT; % Generate the time vector.
x = sin(2*pi*f*t); % Generate the desired 2 Hz sine wave.
subplot(3,1,Ts_i)
plot (t,x,'.k') % Plot the sine wave as discrete points.
xlabel 'Time (s)' % and label the (x) and (y) axes.
ylabel 'x(t)'
end

Community Treasure Hunt

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

Start Hunting!