How to make a scale-able structure array with variable values
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Stephen23
il 17 Ott 2018
Modificato: Stephen23
il 17 Ott 2018
"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 = [...]
...
Risposte (3)
Stephen23
il 17 Ott 2018
"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 Commenti
Stephen23
il 17 Ott 2018
Modificato: Stephen23
il 18 Ott 2018
"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
il 17 Ott 2018
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
il 17 Ott 2018
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.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!