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 日間)
Emily Eastman 2017 年 8 月 29 日

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.
##### 2 件のコメント表示非表示 1 件の古いコメント
José-Luis 2017 年 8 月 29 日
I don't get it. How would switch help here?

### 回答 (1 件)

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