Does MatLab have this kind of function?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
ecastro
Messages
249
Reaction score
8
I have a set of variables that are always inputs for several functions that I made. Does MatLab have a kind of function that stores these variables into a single matrix (or similar) so that I just need to call this matrix for each function rather than calling them one-by-one as inputs into the functions that I made?

I have planned on saving the variables, but it would be inconvenient if I were to save the variables each time their values are updated or replaced. I think Fortran has this function called 'commons'.

Thank you in advance.
 
Physics news on Phys.org
That is indeed possible, but will it possible to just call the variable name(s)? Some of my functions use just a few of them.
For example I have a value of variable 'x' stored in the cell array, and I want to use 'x' in one of my functions. I could use the whole cell array as an input in the function, but I just need 'x', will it be possible to just call the variable name 'x'? I think it might get confusing if I were to memorize each position of each values of my variables.
 
There are several variable types you could use.

There is a cell array. A cell array is like an array of buckets. You can put anything you want into any bucket. And all buckets in a row or column of buckets don't need to have the same type or size of stuff in them.

A related variable that takes up less memory and overhead is a table. A table has columns of data but each column must have the same data type in them, though different columns could have different data types. So you could have a table with a column of name strings and a column of ages and a column of birthdates.

The final variable type you might want to consider is perhaps the easiest to use and it's a structure. It's just like a structure in any other language. You have a structure and fields/members of the structure. Like a structure could be the description of a person and have fields name, age, birthdate, city. You could have an array of those, like one structure for one person and another for another person. So if you had a structure array called "allPeople", the age of person #2 would be "allPeople(2).age" and the city of person #5 would be allPeople(5).city.