필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Stashu Kozlowski
Stashu Kozlowski 2021년 9월 29일
답변: Steven Lord 2021년 9월 29일
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
Stashu Kozlowski
Stashu Kozlowski 2021년 9월 29일
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 2021년 9월 29일
편집: Stephen23 2021년 9월 29일
"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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2021년 9월 29일
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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by