How to Get the File Name and Store a File into a Struct While Using the Function uiopen?

조회 수: 4 (최근 30일)
I want to do two extra things while calling uiopen function.
1) How can I get the file that is opened? For example, I open A.mat and want ‘A’ in the Workspace
2) How can I put all the variables of the opened file right into a struct? For example, I open A.mat and it has B and C variables. MATLAB creates a struct with fields B and C

채택된 답변

Walter Roberson
Walter Roberson 2017년 3월 7일
For those purposes you should use uigetfile() to get the file name and path, fullfile() to put those together into a complete name, and load() on the complete name.
If you assign the output of load() of a .mat to a variable then load() will create a struct with one field per stored variable, the way you describe. This is the recommended approach.
If you do not assign the output of load() of a .mat to a variable, then load() will create one workspace variable for each stored variable from the .mat file, overwriting any existing variables with the same name. It is not possible to "protect" any workspace variable when you do this. The Just In Time optimizer does not know about variables that are "poofed" into existence, so you might get unexpected results. This is not recommended. If you need the contents outside of a structure then you should load() returning a structure like described above, and then specifically pull out the parts you need.
filecontents = load(....)
B = filecontents.B;
C = filecontents.C;
  댓글 수: 2
Rightia Rollmann
Rightia Rollmann 2017년 3월 7일
Thank you!
When I use it I, for example, I get
FileName = A.mat;
FilePath = C:\Users\A\
How to use fullfile() to put them together into a complete name?
Jan
Jan 2017년 3월 7일
Read the documentation:
doc fullfile
There you find:
File = fullfile(FilePath, FileName)

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

추가 답변 (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