Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing "many" variables between functions in Matlab

in Matlab I have several records of a database stored in the matrix DataMatrix. Each row of the matrix is a record and each column is the value of a property of the record. To make the program easy to understand for each column of DataMatrix I defined a variable name explaining what property is associated to the column, that is:

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

I have about 50 columns to name.

My problem is that the values in DataMatrix are used in different functions and I would like to always use the columns name to work on the data in DataMatrix. So I have to share between different functions the values of ColApple, ColOrange, ColLemon, ...

Up to now I thought to two possible approaches:

  1. Making the columns name global

  2. Define a function returning the values for the columns name, that is:

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

I would avoid the global solution because I think it is dangerous and also because I would like the columns name constant if possible. The second approach is better but since I have 50 columns I do not know if it is a good idea to have a function returning 50 different values (also it is difficult to maintain in my opinion).

Anyone has a more robust or maintainable approach to solve this problem? I am sure I am not the first one to deal with this but I was not able to find a solution.

like image 857
MeSS83 Avatar asked Jan 06 '23 18:01

MeSS83


2 Answers

This is perfect for container maps. A container map allows for creating a dictionary. For example

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

will create the dictionary

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

you can find the desired column number with

>> fruits('Orange')

ans =

     2
like image 198
hbaderts Avatar answered Jan 17 '23 08:01

hbaderts


Don't use global variables, as they are prone to errors. Creating 50 variable names is not very error-proof either (see: dynamic variable naming).

I'd go with a simple cell array in this case. You can either read the names from the first row in your data matrix if they are written there, or hand write them if you're doing that now anyway.

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

This will give you a 1 x 50 cell array containing your column names. The names can be found simply by entering the correct column number, i.e. column 22 would be ColumnNames{1,22}. You can now pass the variable ColumnNames to other functions as just a single variable. In order to get the corresponding column name if you do not use dynamic variable naming, but e.g. your colApple, you can use strcmp

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

This way the strcmp checks which cell contains the string 'colApple', and the find returns the index number of the requested cell.

I pass a lot of variables in my own code, which I do in a structure array, since that's able to store all kinds of different data and have a reasonable name for each structure entry:

result.data = [m x 9 double]
result.grid.z = ~[5000 x 5500 double]
result.filename = 'filename.asc'
...
like image 37
Adriaan Avatar answered Jan 17 '23 07:01

Adriaan