フィルターのクリア

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

2 ビュー (過去 30 日間)
Bradley Cory
Bradley Cory 2018 年 6 月 18 日
コメント済み: Bradley Cory 2018 年 6 月 18 日
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 件のコメント
Stephen23
Stephen23 2018 年 6 月 18 日
編集済み: Stephen23 2018 年 6 月 18 日
"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.
Bradley Cory
Bradley Cory 2018 年 6 月 18 日
Thank you

サインインしてコメントする。

回答 (1 件)

Ameer Hamza
Ameer Hamza 2018 年 6 月 18 日
編集済み: Ameer Hamza 2018 年 6 月 18 日
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 件のコメント
Stephen23
Stephen23 2018 年 6 月 18 日
編集済み: Stephen23 2018 年 6 月 18 日
+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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by