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

5 次查看(过去 30 天)
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
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]

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by