필터 지우기
필터 지우기

Loading .mat files with same variable name

조회 수: 16 (최근 30일)
Peyman
Peyman 2015년 8월 13일
댓글: Titus Edelhofer 2015년 8월 14일
I could do with some help.
I have loads of .mat structure arrays that contain 'data' (84x179). I want to be able to plot the data on top of each other but the problem is that since they are all called 'data', they replace each other in my workspace. What is a good way around this? Is there a way I can change 'data' for each .mat file or should I create a for loop. I don't know how to do this so any tips would be most appreciated.
Thanks

채택된 답변

Titus Edelhofer
Titus Edelhofer 2015년 8월 13일
Hi,
the main idea is to use load with a return argument instead of as a command, something like
files = dir('*.mat');
allData = cell(size(files));
for ii=1:length(files)
aFileData = load(files(ii).name);
allData{ii} = aFileData.data;
end
Titus
  댓글 수: 2
Peyman
Peyman 2015년 8월 14일
Thanks Titus. I will try this and let you know how it goes. If I were to change the name of each variable in the .mat file, would it be an easy way of doing it?
Titus Edelhofer
Titus Edelhofer 2015년 8월 14일
Yes, that would be no problem. If you know that it's only one variable, use fieldnames:
aFileData = load(files(ii).name);
% names of the variables stored:
variableNames = fieldnames(aFileData);
% use the first name and "dynamic field names", i.e., (...):
allData{ii} = aFileData.(variableNames{1});

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by