Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How can I create series of equations to graph, that might have a different number of equations every time, without using dynamic variable naming?

1 vue (au cours des 30 derniers jours)
I'm trying to write myself a nice Matlab program that lets me input arguments based on prompts and will graph the equations I put in. I need it to be able to graph anywhere from 1-10 equations at a time, but i cant figure out how to set them all up with names, without naming variables dynamically with something like eval. I know dynamic variable naming is bad practice, but I don't know of another way to do it.
(I am trying to improve my proficiency in mat lab beyond using it as a large graphing calculator, and I know the graphing commands so this lets me learn about script writing and know what I was trying to draw, so I can tell what went wrong)
I would also appreciate knowing if there are multiple methods to achieve this.

Réponses (1)

José-Luis
José-Luis le 29 Août 2017
Modifié(e) : José-Luis le 29 Août 2017
You can place function handles in cell arrays:
funArray = cell(2,1);
funArray{1} = @(x) x.^2;
funArray{2} = @(x) 3.*x + 5;
funArray{1}(3)
funArray{2}(3)
funArray{1}(1:5)
fplot(funArray{1},[-5,5])
In order to parse your inputs to a function handle, you could try using str2func() which is, allegedly, a step above eval().

Community Treasure Hunt

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

Start Hunting!

Translated by