Clear Filters
Clear Filters

How do I save, chaning the name of the file and workspace that I save?

2 views (last 30 days)
I'm looking to save the out put of my project, data. and I can create a file name. But opening that means it just displaces 'data' instead of 'name of X'
filename = Name
Variablename = data
save(strcat(filename,'.mat),veriablename)
I need the veriablename to changed to the filename as well. This should allow me to open more than one at once if im correct?
Thank!
  2 Comments
Stephen23
Stephen23 on 18 Jun 2018
Edited: Stephen23 on 18 Jun 2018
"I need the veriablename to changed to the filename as well. This should allow me to open more than one at once if im correct?"
Sort of: having lots of different names would allow you to open multiple .mat files directly into the workspace, e.g. by double clicking. But this would be an extremely bad approach to take for writing code, because keeping the names the same would significantly simplify your code: it is much easier to load and process the data in a loop, when all the variable names are exactly the same.
Having all the same variable names means that you can trivially load the data into a structure, and then merge/contcatenate/... that data:
for k = 1:nuber of files
S = load(...)
... S.data ...
end
What you are proposing would require reasonably complex code to parse and decipher the fieldnames, and/or writing ugly, slow, buggy code that handles lots of different variable names:
Instead of using different variable names, which makes code complex, slow, and buggy, you should use indexing. Indexing is simple, neat, easy to debug, and very efficient. You should use indexing.

Sign in to comment.

Answers (1)

Ameer Hamza
Ameer Hamza on 18 Jun 2018
Edited: Ameer Hamza on 18 Jun 2018
You should never directly load mat file to the workspace, always load the mat file to a variable (see why). For example
data = load('filename');
So if you want to load several files, you can just do make load them into an array
data(1) = load('filename1');
data(2) = load('filename2');
This will avoid the trouble of conflicting variable names.
  1 Comment
Stephen23
Stephen23 on 18 Jun 2018
Edited: Stephen23 on 18 Jun 2018
+1
Note that to use this simple syntax:
data(1) = load('filename1');
data(2) = load('filename2');
The fieldnames (i.e. variable names in the .mat files) have to be exactly the same. This is why it is strongly recommended to keep the variable names exactly the same in sets of .mat files. If you change the variable names then the code you would need to use is much more (and pointlessly) complex.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!