필터 지우기
필터 지우기

Get the filename of a loaded file

조회 수: 97 (최근 30일)
Fabian Grodek
Fabian Grodek 2022년 8월 3일
댓글: Fabian Grodek 2022년 8월 3일
Hi,
I have a script that loads a file and do some data plotting. I am loading different files each time I run the script. I manually edit the file name since every time the name is different and comming from an unrelated system.
My script reads the file as follows:
load('\Data_record_file_20220714T121615.mat');
Question:
How can I register the file name in a variable? I want to later use the file name in a string (after trimming it) so that I can automatically include the file name on the plot title.
Thank you.
Fabián

채택된 답변

Star Strider
Star Strider 2022년 8월 3일
Save the fille name as a string or character array first, then refer to it as any other variable —
filename = '\Data_record_file_20220714T121615.mat';
load(filename);
One option might be to store all the various file names in a cell array or string array, and then just subscript into it —
load_data = load(filecell{k});
where ‘k’ is the desired element of the cell array ‘filecell’ storing the file names.
It is always best to load into a structure output. This avoids problems such as overwriting existing variables, and other potential problems —
load_data = load(filename);
then refer to the fields of ‘load_data’ (or whatever you choose to call it) to recover the various variables.
.
  댓글 수: 4
Walter Roberson
Walter Roberson 2022년 8월 3일
The file name is not returned when you load(). The only way to find it after the fact is to use dbstack() to locate the file name and line of code that you just executed, and then parse the matlab source for the routine that calls load() and read the quoted string from the source code.
If you have a red box of nails and a blue box of nails and you pull a nail out of one of the boxes, then you know that it is unlikely that the nail will have inscribed on it "This came from the blue box". MAT files are containers, and the loaded data does not get labeled with file name.
Star Strider
Star Strider 2022년 8월 3일
@Fabian Grodek — My pleasure!
@Walter Roberson — Thank you!
.

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

추가 답변 (2개)

桂生 李
桂生 李 2022년 8월 3일
try
[FileName, FilePath] = uigetfile;

Walter Roberson
Walter Roberson 2022년 8월 3일
filename = '\Data_record_file_20220714T121615.mat';
load(filename);
[~, basename, ~] = fileparts(filename);
title( "Analysis of " + basename)
Note: we do not recommend that form of load(). We recommend that you assign the output of load() to a variable, which gives you a struct array with one field for each loaded variable; you would then pull fields out of the struct as needed. This is more robust and more efficient, and is needed if you later want to do code generation. There are situations in which MATLAB will ignore variables created by load() that does not have an output variable name.
  댓글 수: 3
Walter Roberson
Walter Roberson 2022년 8월 3일
load(filename); is not recommended. load_data = load(filename); is recommended instead.
Fabian Grodek
Fabian Grodek 2022년 8월 3일
Understood. Thanks.

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

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by