Info

此问题已关闭。 请重新打开它进行编辑或回答。

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 次查看(过去 30 天)
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.

回答(1 个)

José-Luis
José-Luis 2017-8-29
编辑:José-Luis 2017-8-29
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