How to print NaN for unexist value instead of error message

10 vues (au cours des 30 derniers jours)
Mahome
Mahome le 2 Juil 2017
Commenté : Star Strider le 2 Juil 2017
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"

Réponse acceptée

Star Strider
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
Mahome
Mahome le 2 Juil 2017
Thank you so much for help, that's what I'm looking...
Star Strider
Star Strider le 2 Juil 2017
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by