MATLAB Does MatLab have this kind of function?

Click For Summary
MatLab offers several options for managing multiple variables efficiently within functions. Users can utilize cell arrays, which allow for storing various data types in a flexible manner, akin to "buckets." This enables easy access to individual variables, but the challenge remains in referencing them by name without memorizing their positions. Alternatively, tables can be used, which organize data in columns of the same type, allowing for structured data management but with less flexibility than cell arrays. Structures present another option, functioning similarly to structures in other programming languages, where fields can be accessed directly by name, making them user-friendly for managing related data. Each of these approaches provides a way to streamline variable management in MatLab, reducing the need to repeatedly input individual variables into functions.
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K