# Why do I receive a subplot error?

3 views (last 30 days)
Kamryn Calloway on 8 Oct 2021
Commented: Star Strider on 9 Oct 2021
figure(1)
hxl= subplot(scatter(height,leg));
lsline(hxl);
polyfit(height,leg,1);
title('Height v. Leg Length');
xlabel('Height (cm)');
ylabel('Leg Length (cm)');
%figure(2)
hxf= subplot(scatter(height,foot));
lsline(hxf);
polyfit(height,foot,1);
title('Height v. Foot Length');
xlabel('Height (cm)');
ylabel('Foot Length (cm)');
%figure(3)
hxs= subplot(scatter(height,speed));
lsline(hxs);
polyfit(height,speed,1);
title('Height v. Speed');
xlabel('Height (cm)');
ylabel('Speed (m/s)');
%figure(4)
lxs= subplot(scatter(leg,strl));
lsline(lxs);
polyfit(leg,strl,1);
title('Leg Length v. Stride Length');
xlabel('Leg Length (cm)');
ylabel('Stride Length (distance/stride)');
% Find the R^2 and p-value for each linear fit.
% Use command fitlm(x,y)
fitlm(height,leg)
fitlm(height,foot)
fitlm(height,speed)
fitlm(leg,strl)
>> WalkingSpeed
Error using subplot (line 197)
Requires valid axes handle for input.
Error in WalkingSpeed (line 49)
subplot(scatter(height,leg));

Star Strider on 8 Oct 2021
That’s not how subplot works.
It should be something like this —
figure(1)
hxl = subplot(2,2,1);
scatter(height,leg)
lsline
% polyfit(height,leg,1);
title('Height v. Leg Length')
xlabel('Height (cm)')
ylabel('Leg Length (cm)')
%figure(2)
hxf = subplot(2,2,2);
scatter(height,foot)
lsline
% polyfit(height,foot,1);
title('Height v. Foot Length')
xlabel('Height (cm)')
ylabel('Foot Length (cm)')
%figure(3)
hxs= subplot(2,2,3);
scatter(height,speed)
lsline
% polyfit(height,speed,1);
title('Height v. Speed')
xlabel('Height (cm)')
ylabel('Speed (m/s)')
%figure(4)
lxs = subplot(2,2,4);
scatter(leg,strl)
lsline
% polyfit(leg,strl,1);
title('Leg Length v. Stride Length')
xlabel('Leg Length (cm)')
ylabel('Stride Length (distance/stride)')
That should work, although it may be necessary to tweak it to get the desired result.
.
##### 2 CommentsShowHide 1 older comment
Star Strider on 9 Oct 2021
As always, my pleasure!
.

R2021a

### Community Treasure Hunt

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

Start Hunting!