Reputation: 21583
I have a set of constants
case 'a'
h_grad = 300;h_alpha = 0.11;
case 'b'
h_grad = 350;h_alpha = 0.15;
case 'c'
h_grad = 450;h_alpha = 0.22;
case 'd'
h_grad = 550;h_alpha = 0.3;
How can I construct them, so I can use them like a.h_grad
, a.h_alpha
, or b.h_grad
, b.h_alph
. ?
And I may want to use these data like:
v = custom_function(a)
% surface_type could be a, b, c or d
function v = custom_function(surface_type)
h_grad = surface_type.h_grad;
h_alpha = surface_type.h_alpha;
v = h_grad^h_alpha
end
And, since a/b/c/d may be used in other cases for other variable, how can I put these predefined a.h_grad constant into another object? So they wont pollute the who program.
For example, I may re-use them like My_Constant_Containter.a.h_grad
?
Upvotes: 1
Views: 63
Reputation: 221684
You can use deal
(Distribute inputs to outputs
) here -
%// Define arrays to hold the scalars
h_grad_vals = [300 350 450 550];
h_alpha_vals = [0.11 0.15 0.22 0.3];
h1 = num2cell(h_grad_vals)
[a.h_grad,b.h_grad,c.h_grad,d.h_grad] = deal(h1{:})
h2 = num2cell(h_alpha_vals)
[a.h_alpha,b.h_alpha,c.h_alpha,d.h_alpha] = deal(h2{:})
Output -
a =
h_grad: 300
h_alpha: 0.1100
b =
h_grad: 350
h_alpha: 0.1500
c =
h_grad: 450
h_alpha: 0.2200
d =
h_grad: 550
h_alpha: 0.3000
Rather than saving to four different structs, for easy portability of codes and code maintenance, it would make more sense to have these scalars stored as struct of arrays. So, you can do this -
A.h_grad = h_grad_vals %// h_grad_vals from earlier code
A.h_alpha = h_alpha_vals %// h_alpha_vals from earlier code
Thus, you would be get -
A =
h_grad: [300 350 450 550]
h_alpha: [0.1100 0.1500 0.2200 0.3000]
Leaving you with just one variable A
and thus, a much cleaner workspace!
Here's a fix to solve the question based on the new info provided in the Edit section -
A = cat(1,a,b,c,d);
fns = {'a','b','c','d'};
for k = 1:numel(fns)
My_Constant_Containter.(char(fns(k))) = A(k);
end
Here's the data store in the output struct looks like -
>> My_Constant_Containter.a
ans =
h_grad: 300
h_alpha: 0.1100
>> My_Constant_Containter.b
ans =
h_grad: 350
h_alpha: 0.1500
>> My_Constant_Containter.c
ans =
h_grad: 450
h_alpha: 0.2200
>> My_Constant_Containter.d
ans =
h_grad: 550
h_alpha: 0.3000
Upvotes: 2
Reputation: 1641
Are you looking for something like this:
a.h_grad = 300;a.h_alpha = 0.11;
b.h_grad = 350;b.h_alpha = 0.15;
c.h_grad = 450;c.h_alpha = 0.22;
d.h_grad = 550;d.h_alpha = 0.3;
They are called structure type variables.
The case
words are not clear to me, are you using switch
?
EDIT: Based on the new info you provided, one possibility if you want to keep the structure variables (CC-constant container):
CC.a.h_grad = 300;CC.a.h_alpha = 0.11;
CC.b.h_grad = 350;CC.b.h_alpha = 0.15;
CC.c.h_grad = 450;CC.c.h_alpha = 0.22;
CC.d.h_grad = 550;CC.d.h_alpha = 0.3;
surface_type = 'b';
function v = custom_function(surface_type)
h_grad_i = CC.(surface_type).h_grad;
h_alpha_i = CC.(surface_type).h_alpha;
v = h_grad_i^h_alpha_i;
end
Upvotes: 2