# Error using zeros, Size inputs must be scalar. Need help with this error in my function.

45 views (last 30 days)
Alan Barraza on 27 Oct 2020
Answered: Sudhakar Shinde on 27 Oct 2020
My function runs successfully at first, but once i type in a length of vector i get this error:
This program accepts a vector of masses and calculates the energy for
them. It displays a linear and three logarithmic plots and provides a
table of values for mass entered and resulting energy from the row vector
entered.
Enter your choice 1.Metric 2.US customary units : 1
Enter your choice 1.Vector 2.Scaler : 1
Enter the length of the vector : [100 200 300 400 500]
Error using zeros
Size inputs must be scalar.
Error in energy2 (line 20)
m = zeros(1,ln);
Here is my full function:
function[E]=energy
% This program accepts a vector of masses and calculates the energy for
% them. It displays a linear and three logarithmic plots and provides a
% table of values for mass entered and resulting energy from the row vector
% entered.
%
clc
close all
help energy
c=2.9979e8;
unt_choice = input('Enter your choice 1.Metric 2.US customary units : ');
if unt_choice == 1
vctr = input('Enter your choice 1.Vector 2.Scaler : ');
if vctr == 2
E=m*c^2
fprintf(E)
else
ln = input('Enter the length of the vector : ');
m = zeros(1,ln);
q = 1
fprintf('Start Entering your vector of masses in Kg')
while q<ln
m(q) = input('...');
q =q+1;
end
E=m.*c^2;
subplot(2,2,1)
plot(m,E,'b-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J)')
title('E vs m-NORMAL')
grid
subplot(2,2,2)
semilogx(m,E,'m-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J)')
title('E vs m-SEMILOGX')
grid
subplot(2,2,3)
semilogy(m,E,'r-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-SEMILOGY')
grid
subplot(2,2,4)
loglog(m,E,'g-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-LOGLOG')
grid
filename = 'energy.xlsx';
A = {'Mass (kg)';'Energy (J)'};
xlswrite('energy.xlsx', A, 1, 'A1');
xlswrite('energy.xlsx', [m; E], 1, 'B1');
end
else
vctr = input('Enter your choice 1.Vector 2.Scaler : ');
if vctr == 2
E=m*14.5939*c^2
fprintf(E)
else
ln = input('Enter the length of the vector : ');
m = zeros(1,ln);
q = 1
fprintf('Start Entering your vector of masses in slug')
while q<ln
m(q) = input('...');
q =q+1;
end
m = m.*14.5939
E=m.*c^2;
subplot(2,2,1)
plot(m,E,'b-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J)')
title('E vs m-NORMAL')
grid
subplot(2,2,2)
semilogx(m,E,'m-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J)')
title('E vs m-SEMILOGX')
grid
subplot(2,2,3)
semilogy(m,E,'r-p')
xlabel('MASS (kg)')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-SEMILOGY')
grid
subplot(2,2,4)
loglog(m,E,'g-p')
xlabel('MASS (kg) IN LOG SCALE')
ylabel('ENERGY (J) IN LOG SCALE')
title('E vs m-LOGLOG')
grid
filename = 'energy.xlsx';
A = {'Mass (kg)';'Energy (J)'};
xlswrite('energy.xlsx', A, 1, 'A1');
xlswrite('energy.xlsx', [m; E], 1, 'B1');
end
end

Sudhakar Shinde on 27 Oct 2020
If you are inputing,
1. Enter the length of the vector : 2 (any scalar value) then it will work
m = zeros(1,length(ln))
2. Enter the length of the vector : [1 2 3 4] , then you can use:
m = zeros(1,length(ln))

### Community Treasure Hunt

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

Start Hunting!