Error using fprintf Invalid file identifier. Use fopen to generate a valid file identifier. Function help
1 view (last 30 days)
Show older comments
When i run the function and enter the following outputs, i get this error message:
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 : 2
Please enter mass in Kg=10
E =
8.9874e+17
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in energy (line 17)
fprintf(E)
Here is the full function code:
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
m=input('Please enter mass in Kg=');
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
m=input('Please enter mass in slug=');
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
0 Comments
Answers (2)
Star Strider
on 28 Oct 2020
Try this:
fprintf('E = %23.15E\n',E)
Choose the format you want.
0 Comments
Asad (Mehrzad) Khoddam
on 28 Oct 2020
Because E is a numeric variable, it conot be used directly in your case.
Use E with some formatting:
fprintf('%g\n',E)
0 Comments
See Also
Categories
Find more on Device Characteristics Assessment in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!