Sharing "many" variables between functions in Matlab

in Matlab I have several database records stored in a matrix DataMatrix. Each row of the matrix is ​​a record, and each column is a value of a record property. To make the program understandable for each column DataMatrix, I defined a variable name that explains which property is associated with the column, that is:

ColApple = 1;
ColOrange = 2;
ColLemon = 3;
...

I have about columns 50for a name.

My problem is that the values ​​in DataMatrixare used in different functions, and I would like to always use the column name to work with the data in DataMatrix. So I have to be shared between different values of functions ColApple, ColOrange, ColLemon,...

So far I have been thinking about two possible approaches:

  • Creating global column names
  • Define a function that returns values ​​for the column name, namely:

    [ColApple, ColOrange, ColLemon, ... ] = getColNames

I would avoid a global solution because I think it is dangerous, and also because I would like the column name to be constant, if possible. The second approach is better, but since I have columns 50, I don’t know if there is a good idea for the function to return 50different values ​​(this is also difficult to maintain in my opinion).

Does anyone have a more reliable or supported approach to solve this problem? I am sure that I am not the first to deal with this, but I could not find a solution.

+4
source share
2 answers

. .

fruits = containers.Map({'Apple', 'Orange', 'Lemon'}, [1, 2, 3])

'Apple'   ->   1
'Orange'  ->   2
'Lemon'   ->   3

>> fruits('Orange')

ans =

     2
+7

, . 50 (.: ).

cell array. , , , .

ColumnNames = {'Col1', 'Col2', 'Col3', (...) , 'Col 50'}

1 x 50, . , 22 ColumnNames{1,22}. ColumnNames . , , , , colApple, strcmp

ColIdx = find(strcmp(ColumnNames,'colApple'));

, strcmp , 'colApple', find .

, , :

result.data = [m x 9 double]
result.grid.z = ~[5000 x 5500 double]
result.filename = 'filename.asc'
...
+2

Source: https://habr.com/ru/post/1625610/


All Articles