Does MatLab have this kind of function?

In summary, the conversation is about finding a way to store a set of variables that are used as inputs for multiple functions in MatLab. The person mentions Fortran's 'commons' function and asks if there is a similar function in MatLab. The expert suggests using a cell array, table, or structure as variable types to store the variables. The person asks if it is possible to call a specific variable from the cell array, and the expert explains the different types of variables and how they can be used.
  • #1
ecastro
254
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
  • #3
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.
 
  • #4
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.
 

Related to Does MatLab have this kind of function?

What is MatLab?

MatLab is a high-level programming language and interactive environment used by scientists, engineers, and mathematicians for data analysis, visualization, and numerical computation.

Does MatLab have built-in functions?

Yes, MatLab has a vast library of built-in functions that cover a wide range of mathematical, statistical, and engineering tasks. These functions can save time and effort by providing pre-written code for common tasks.

Can I create my own functions in MatLab?

Yes, MatLab allows users to create their own functions using the function keyword. These functions can be saved and reused in different scripts or applications.

Does MatLab have a function for plotting data?

Yes, MatLab has a variety of functions for visualizing data, including plot, scatter, bar, and histogram. These functions can create 2D and 3D plots with customizable features such as labels, colors, and styles.

Is MatLab suitable for statistical analysis?

Yes, MatLab has a powerful set of built-in functions for statistical analysis, such as mean, median, standard deviation, and regression. It also has specialized toolboxes for more advanced statistical methods.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
977
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
Back
Top