필터 지우기
필터 지우기

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.

댓글을 달려면 로그인하십시오.

답변 (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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by