Azzera filtri
Azzera filtri

How to create variable names and then declare them as global in a function

1 visualizzazione (ultimi 30 giorni)
I have multiple global varibles that come in triplets of the form:
f1, f1_max, f1_min
f2, f2_max, f2_min
f3, f3_max, f3_min
... and so on ...
I am trying to create a function that only takes in the input fx and will output the four values that are determined by a series of calculations using the corresponding 3 global values. Because I am inputting only one value, fx, I need a way to declare fx_max and fx_min as global variables. In other words, I need first to assemble those variables and then somehow declare them as global. I thought about using the eval function like this:
function [avg_year, total, start_date, end_date] = avg_func(name_of_func)
global eval(append(string(name_of_func), '_max')) eval(append(string(name_of_func), '_min'))
% rest of func
end
This however fails and returns the following message:
Error: File: avg_func.m Line: 2 Column: 27
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise,
check for mismatched delimiters.
Does anyone else have a alternative method?
  2 Commenti
Stashu Kozlowski
Stashu Kozlowski il 29 Set 2021
I have thought about doing something like this:
f1 = [f1, f1_max, f1_min]
This, however, is not an ideal solution as it requires me to manipulate the properties of variables that I will use for other calculations.
Stephen23
Stephen23 il 29 Set 2021
Modificato: Stephen23 il 29 Set 2021
"Does anyone else have a alternative method?"
Using global variables is a sign that most likely you are doing something wrong.
Numbering variables like that is a sign that most likely you are doing something wrong.
Is there any good reason why you cannot use basic arrays+indexing, just like MATLAB was design for?

Accedi per commentare.

Risposte (1)

Steven Lord
Steven Lord il 29 Set 2021
Consider using a struct array. This can encapsulate the related data into one variable that can be easily passed into functions. Functions can have the same signature (just accept the struct as input) but can use any or all of the fields in the struct as they choose. Note that even though s1 and s2 in the example below contain different data, the same displayValues function can handle each of them.
I'm using dynamic field names to refer to the contents inside the struct array.
s1 = struct('name', 'f', 'f', 42, 'f1', -99, 'f1x', 1);
s2 = struct('name', 'z', 'z', 88, 'z1', 5, 'z1x', -12345);
displayValues(s1)
The struct variable contains f = 42, f1 = -99, and f1x = 1.
displayValues(s2)
The struct variable contains z = 88, z1 = 5, and z1x = -12345.
function displayValues(f)
N = f.name;
fprintf('The struct variable contains %s = %d, %s = %d, and %s = %d.\n', ...
N, f.(N), N + "1", f.(N + "1"), N + "1x", f.(N + "1x"));
end

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by