How to print NaN for unexist value instead of error message
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Is there any way to print NaN instead of error message due to the two matrix are not valid.. For example, in this simple codes
clc, clear all
x = [1,2,3,4,5];
y = [3,4,5,6,7];
for i = 1: length(x)
out = x(i)+y(i+1);
fprintf('%.0f \t', out)
end
On other hand, please do not recommended me to do like this,
clc, clear all
x = [1,2,3,4,5];
y = [3,4,5,6,7];
for i = 1: length(x)-1
out = x(i)+y(i+1);
fprintf('%.0f \t', out)
end
fprintf('NaN \t')
---- result should be
5 7 9 11 NaN
I'm looking for trick which is fancy and more professional to fill unexcist variable by printing word like "NaN"
0 commentaires
Réponse acceptée
Star Strider
le 2 Juil 2017
Try this:
x = [1,2,3,4,5];
y = [3,4,5,6,7];
for i = 1: length(x)
try
out = x(i)+y(i+1);
catch
out = NaN;
end
fprintf('%.0f \t', out)
end
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!