Getting variable names from .mat files when they are loaded inside a function

In summary, The conversation is about a question regarding a MATLAB function that loads data, performs some operations, and plots the results. The issue is that the function is unable to get the variable names from the loaded .mat files. The solution is to use the "who" or "whos" command to get the list of variables and then use the "evalin" command to assign the values to the desired variables.
  • #1
pxfy
1
0
hi all, I got a simple MATLAB question here.

I am writting a function which basicaly load my data (in .mat format), do something to them, and then plot them. The function is something like:

for i=1:n
[name1, path1]=uigetfile('*.mat','load data file')
load([path1 name1]);
end

new_data= do_stuff (variable names from .mat files)
plot(new_data)


The problem here is that after I have loaded the .mat files, I can't find a way to make MATLAB get the variable names!



Can anyone help out? Much appreciated!
 
Physics news on Phys.org
  • #2
You can use the "who" or "whos" command to get the list of variables in the currently active workspace. You can then use the "evalin" command to evaluate a string that contains the name of a variable. For example: varname = 'variable_name'; variable_data = evalin('base',varname); This will assign the value of the variable 'variable_name' in the base workspace to the variable 'variable_data'.
 

What is a .mat file?

A .mat file is a file extension used by MATLAB to store variables in a binary format. It is commonly used for saving and loading data in scientific research or data analysis.

How do I load a .mat file into a function in MATLAB?

To load a .mat file into a function, you can use the load function in MATLAB. This will load all the variables from the .mat file into the workspace of the function.

How do I get the names of variables from a .mat file loaded into a function?

To get the names of variables from a .mat file loaded into a function, you can use the whos function in MATLAB. This will display a list of all the variables in the workspace, along with their names and sizes.

Can I access the variables from a .mat file loaded into a function outside of the function?

Yes, you can access the variables from a .mat file loaded into a function outside of the function. However, you will need to use the dot notation to specify the function name and the variable name, such as function_name.variable_name.

Is it possible to change the names of variables from a .mat file loaded into a function?

No, it is not possible to change the names of variables from a .mat file loaded into a function. The names of variables are fixed when they are saved in a .mat file and cannot be changed during loading or accessing.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
Back
Top