How I can to get the name of a file.mat when i load it?

조회 수: 11 (최근 30일)
Abel Romero
Abel Romero 2018년 1월 4일
댓글: Steven Lord 2022년 10월 22일
Hello, i want to get the name of a file.mat when i load it. For example, I load 'Resonator.mat', how i can to get the string 'Resonator' when i load it?
I know how to load a .mat file and how to save it, but i need the get the name because i want to overwrite the variables of the .mat and save with the same name(i have a lot of .mat and i can't fix the name).

채택된 답변

Vishwas Kumar
Vishwas Kumar 2018년 1월 9일
You can use something like uigetfile
fileName = uigetfile('*.mat');
load(fileName)
filename = filename(1:end-4)
Or alternatively, you can do some batch processing doing something like:
dirinf = dir('*.mat');
nfiles = length(dirinf);
for n = 1:nfiles
infile = dirinf(n).name; % extract one file name from struct
load(infile) % load the data
infile = infile(1:end-4);
end
  댓글 수: 2
ELLER JUCO
ELLER JUCO 2022년 10월 22일
how do you find the size from the fileName?
m=size(fileName) - does not work
Steven Lord
Steven Lord 2022년 10월 22일
Find the size of what from the file name?
The size of the file on disk? Use dir.
locationOfCensusMat = which('census.mat')
locationOfCensusMat = '/MATLAB/toolbox/matlab/demos/census.mat'
D = dir(locationOfCensusMat)
D = struct with fields:
name: 'census.mat' folder: '/MATLAB/toolbox/matlab/demos' date: '14-Mar-2004 15:32:00' bytes: 353 isdir: 0 datenum: 7.3202e+05
The sizes of the variables in the file? Use whos.
W = whos('-file', locationOfCensusMat)
W = 2×1 struct array with fields:
name size bytes class global sparse complex nesting persistent
W(1)
ans = struct with fields:
name: 'cdate' size: [21 1] bytes: 168 class: 'double' global: 0 sparse: 0 complex: 0 nesting: [1×1 struct] persistent: 0

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

추가 답변 (1개)

Abel Romero
Abel Romero 2018년 1월 13일
Thank you for your help, it was what i needed-

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by