フィルターのクリア

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

11 ビュー (過去 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 Center および File ExchangeData 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