Does MatLab have this kind of function?

Click For Summary

Discussion Overview

The discussion revolves around the functionality of MatLab in managing multiple variables as inputs for user-defined functions. Participants explore options for storing these variables in a way that allows for easier access without needing to call each variable individually.

Discussion Character

  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant inquires about a MatLab function that can store multiple variables into a single matrix or similar structure for easier function calls.
  • Another participant suggests using a cell array as a potential solution for storing the variables.
  • A follow-up question is raised regarding the ability to access individual variables by name from within a cell array, expressing concern about memorizing positions of values.
  • A participant explains various variable types in MatLab, including cell arrays, tables, and structures, detailing their characteristics and potential uses.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, indicating that there is no consensus on a single best solution. The discussion remains open with various suggestions and considerations.

Contextual Notes

Participants discuss the limitations of each variable type, including memory usage and data type restrictions, but do not resolve the specific needs of the original inquiry.

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
4K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · 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 1 ·
Replies
1
Views
3K