필터 지우기
필터 지우기

Opening mat files with uiopen and copying data to array

조회 수: 4 (최근 30일)
banco freno
banco freno 2019년 4월 17일
댓글: Walter Roberson 2019년 4월 18일
I have some mat files organized as cell arrays. All my mat files have the same structure and have progressive names, like MyFile_001, MyFile_002 and so on.
Copying data to arrays is an easy operation, such as
load('MyFile_001.mat')
Data1 = MyFile_001.Y(1).Data;
Data2 = MyFile_001.Y(2).Data;
but what if I want to choose which mat file to open instead of 'MyFile_001'?
The first row becomes
uiopen('load')
so I choose through GUI the file to be opened. How can I save data to arrays now?
  댓글 수: 1
Stephen23
Stephen23 2019년 4월 17일
It is not clear from your question: do the structures (very unfortunately) all have the same names as the file names (e.g. MyFile_001), or do they (sensibly) all have the same name (e.g. Y)?

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

채택된 답변

Stephen23
Stephen23 2019년 4월 18일
편집: Stephen23 2019년 4월 18일
It seems that whoever created those .mat files unfortunately named each structure with the same name as the filename, which just makes accessing the data more complex (it is much simpler to process a sequence of .mat files when their variable names are the same for each file).
Assuming that each .mat file contains exactly one variable (i.e. the structure), then you can work with these badly designed .mat files by ignoring the variable name entirely:
T = load(...);
T = struct2cell(T);
Data1 = T{1}.Y(1).Data;
Data2 = T{1}.Y(2).Data;
See also:

추가 답변 (2개)

Bob Thompson
Bob Thompson 2019년 4월 17일
It seems like your issue isn't so much opening specific files, but identifying new variables. With a bit of trickery you can identify what variables are new using who.
A = who;
uiopen;
Aprime = who;
new = cellfun(@(x) sum(ismember(A,x)),Aprime,'UniformOutput',0);
new = [Aprime([new{:}]~=1)];
  댓글 수: 1
Stephen23
Stephen23 2019년 4월 17일
Introspective programming (e.g. who) is slow and complex. See:
Simpler and more efficient to simply load into an output variable.

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


Walter Roberson
Walter Roberson 2019년 4월 17일
[filename, pathname] = uigetfile('*.mat');
if isnumeric(filename); error('cancel'); end
fullname = fullfile(pathname, filename);
datastruct = load(fullname, 'Y');
Data1 = datastruct.Y(1).Data;
Data2 = datastruct.Y(2).Data;
  댓글 수: 4
Stephen23
Stephen23 2019년 4월 18일
편집: Stephen23 2019년 4월 18일
"The mat files contain many fields and Y is one of them. Why do I get this message?"
Because this answer does not take into account the description of your .mat files, which apparently contain nested structures with the variable name the same as the filename (i.e. the files contain badly designed data).
Walter Roberson
Walter Roberson 2019년 4월 18일
[filename, pathname] = uigetfile('*.mat');
if isnumeric(filename); error('cancel'); end
fullname = fullfile(pathname, filename);
datastruct = load(fullname);
fn = fieldnames(datastruct); %data is stored in a struct named for the file
Data1 = datastruct.(fn{1}).Y(1).Data;
Data2 = datastruct.(fn{1}).Y(2).Data;

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

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by