Attempting to use complex variable results in input array non-numeric error for polyval

1 vue (au cours des 30 derniers jours)
Say I have polynomial coefficients saved as
G11 = [1, 2, 3, 4];
and I wish to call polyval on said coefficients, except the nomenclature is made up of different variables as GXY where X equals group and Y equals day
group = 1;
day = 1;
I can run
sprintf('G%g%g', group, day);
ans =
'G11'
but when I run it with polyval I get an error
polyval(sprintf('G%g%g', group, day), 1);
Error using filter
Invalid data type. Input arrays must be numeric or logical.
Error in polyval (line 56)
y = filter(1,[1 -x],p);
I'm not sure how to provide an appropriate input for polyval. I have tried num2str and some other ideas. The reason I am doing this is because I have 6 polynomials to loop through to verify a value is out of a certain range before recording a final value.
Thanks

Réponse acceptée

Jan
Jan le 19 Mai 2019
Modifié(e) : Jan le 19 Mai 2019
Of course neither the char vector 'G11' nor any tricks with num2str can solve the problem. But hiding indoices in the names of variables is a bad idea at all. See TUTORIAL: How and why to avoid EVAL .
Use an array and indices instead:
G{1, 1} = [1,2,3,4];
group = 1;
day = 1;
polyval(G{group, day}, 1);

Plus de réponses (1)

Jam
Jam le 19 Mai 2019
Thanks for that Jan, worked like a treat. Need to make mistakes to learn I suppose. I hadn't quite connected my "relelvant variable naming" to indices so to speak.
Cheers,
J

Catégories

En savoir plus sur Elementary Math 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