How to make a scale-able structure array with variable values

3 次查看(过去 30 天)
Hi, I'd like to know if there is a way of doing this with structured arrays or if there is a better approach.
Essentially, I have the following:
CORNER.one = (length,400,radius,900);
But, the radius value needs to be based on the length value. Is it possible to put a maths expression in there, and if so how? All my attempts have resulted in invalid expressions.
Or, is there a better approach. Later on in the script, I need to access each individual radius in further calculations, so, need it to have a variable name.
For example, is there a foreach statement or loop I can use with this, so, for each corner, calculate the radius and give it a variable name with a unique number (1, 2, 3 etc...). As there could be upto 100 corners, so, making the code lightweight and scale-able is desirable.
Thanks.
  2 个评论
Stephen23
Stephen23 2018-10-17
编辑:Stephen23 2018-10-17
"is there a foreach statement or loop I can use with this, so, for each corner, calculate the radius and give it a variable name with a unique number (1, 2, 3 etc...). As there could be upto 100 corners, so, making the code lightweight and scale-able is desirable."
Then changing the variable name is entirely the wrong approach, because that will force you into writing slow, complex, buggy code that is hard to debug. It would be the exact opposite of "lightweight and scale-able" code. Read this to know why:
Instead of magically accessing variable names you should use indexing. Indexing is simple, neat, very efficient, scales extremely well, is easy to debug, and is lightweight. Note that you can easily define a non-scalar structure to hold your data:
CORNER(1).one = [...]
CORNER(2).one = [...]
CORNER(3).one = [...]
...
Nick Deeley
Nick Deeley 2018-10-17
Thanks for the reply. Makes sense that my original thoughts were indeed opposite of what I liked to achieve.
Are there any good examples of indexing which follows roughly what I'm after? Would using matrices work at all? Or converting a matrix to a structured array and holds all the information in one variable?
My knowledge of MATLAB is very basic, so, forgive me for any ignorance of things that should be obvious :)
Thanks.

请先登录,再进行评论。

回答(3 个)

Stephen23
Stephen23 2018-10-17
"Is it possible to put a maths expression in there, and if so how?"
You could define it as a function:
>> S.fun = @(len)[len,400,len*2,900];
>> S.fun(100)
ans =
100 400 200 900
  3 个评论
Stephen23
Stephen23 2018-10-17
编辑:Stephen23 2018-10-18
"The corner lengths will need to be inputted by the user, and the radii worked from that."
That is what my answer lets you do: the user inputs the length, and the radius is worked out from that.
"Are there any good examples of indexing which follows roughly what I'm after? Would using matrices work at all? Or converting a matrix to a structured array and holds all the information in one variable?"
Possibly your task could be achieved using simpler MATLAB concepts, e.g. arrays and numeric operations. But I have no idea, because your description is too vague to know what you are actually trying to achieve. If you want more help, describe what the task is, rather than your attempts or concepts of how to achieve that task.
Guillaume
Guillaume 2018-10-17
I have to concur with Stephen, a better description of the original problem would be useful.
"I would like to perform a maths equation on each radii, giving a result for each radii. Ideally these could be plotted in a graph, but, also need to work out an average of them all to use elsewhere." That sounds like a very common use of matlab. I dom't really see how this relates to the original question.
radii = 100:100:5000
area = pi * radii .^ 2;
plot(radii, area);
mean(area)

请先登录,再进行评论。


Guillaume
Guillaume 2018-10-17
I'm not entirely clear on the reasons behind your question and it does sound like you're trying to fit the language around an ill-defined concept which going to force you in more complicated code that it needs to be. Certainly, as Stephen said, numbered variables are a clear indication that your design is very wrong.
It is possible to achieve what you're asking if you use classes instead of structures. Classes have a concept of dependent properties, properties that are calculated on demand based on the values of other properties, e.g.:
classdef democlass
properties
length;
end
properties (Dependent)
radius;
end
methods
function this = democlass(length) %constructor
if nargin < 1, length = 0; end
this.length = length;
end
function radius = get.radius(this)
radius = this.length / (2 * pi);
end
end
end
Usage:
>> o = democlass
o =
democlass with properties:
length: 0
radius: 0
>> o.length = 200
o =
democlass with properties:
length: 200
radius: 31.8310
>> o = democlass(500)
o =
democlass with properties:
length: 500
radius: 79.5775
There are plenty of reasons to use classes however, I'd say that if it's just so you can have dependent variables then it's a bad idea.
  1 个评论
Nick Deeley
Nick Deeley 2018-10-17
I have no idea how to use classes or whether they'd be any use for this.
I would like to perform a maths equation on each radii, giving a result for each radii. Ideally these could be plotted in a graph, but, also need to work out an average of them all to use elsewhere.

请先登录,再进行评论。


Nick Deeley
Nick Deeley 2018-10-22
编辑:Stephen23 2018-10-24
Have decided to open up a new thread, start back from the beginning and go from there rather than trying to start again half way through this one.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by