필터 지우기
필터 지우기

How to name and save the results using the name of imported file ?

조회 수: 3 (최근 30일)
Ahmad Al-Issa
Ahmad Al-Issa 2024년 4월 14일
편집: Ahmad Al-Issa 2024년 4월 14일
Hello everyone,
I have many files containing data. Each file has a different name based on the date for example (data_2020_03_12_08_30_03.mat).
I want to perform my calculations and save my results in a variable with the same name as the imported file for example (Tempreture_data_2020_03_12_08_30_03.mat).
just you to know I do not want to import all data files at once. I am doing this manualy. I want to import only one file every time but I want the results save in a diffrent name every time depend on the name of imported file.
Could you help me how to name and save the results using the name of imported file??
  댓글 수: 3
Ahmad Al-Issa
Ahmad Al-Issa 2024년 4월 14일
thank you for explanation
Ahmad Al-Issa
Ahmad Al-Issa 2024년 4월 14일
yes you are right. I changed the file name to (data_2020_03_12_08_30_03). there is typos.

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

답변 (1개)

Stephen23
Stephen23 2024년 4월 14일
편집: Stephen23 2024년 4월 14일
A much better approach is to store the data in e.g. a structure array:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = load(F);
end
All of your imported data are contained in the structure S, for example the 2nd file:
S(2).name % filename
S(2).data % imported data (as a scalar structure)
If all of the files contain the same variables then you can easily concatenate them into one non-scalar structure with all of those variables as fields:
D = [S.data] % optional
That makes your data easy to access:
You did not tell us anything about the content of those MAT files (e.g. how many variables they contain, what their names are), so this advice remains quite general.
  댓글 수: 15
Stephen23
Stephen23 2024년 4월 14일
편집: Stephen23 2024년 4월 14일
Some notes on your code:
  • Do not use PATH as a variable name.
  • LOAD is preferred for MAT files. IMPORTDATA is best avoided.
  • Prefer using FULLFILE rather than STRCAT on filepaths.
Try something like this. Before you import any files define a counter and an empty structure:
N = 0;
S = struct('name',{},'data',{});
Then for each file that you "manually" import:
[F,P] = uigetfile('*.mat', 'Select a *.mat-file');
D = load(fullfile(P,F));
D.filename = fullfile(P,F); % optional (but useful if you concatenate the data together)
N = N+1;
S(N).data = D;
S(N).name = fullfile(P,F);
All of the imported file data will be stored in the structure S. You can trivially access all of the filenames and imported filedata using basic indexing. For example, for the 2nd file:
S(2).name % the filename (which can include minus and dot characters !)
S(2).data % the imported file data (a scalar structure)
Note how by design this is more robust than your approach. If all of the MAT files contain the same variables, you can easily create one non-scalar structure of all of the imported data:
D = [S.data]
Ahmad Al-Issa
Ahmad Al-Issa 2024년 4월 14일
편집: Ahmad Al-Issa 2024년 4월 14일
thank you, sir,

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

카테고리

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

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by