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

5 vues (au cours des 30 derniers jours)
Ahmad Al-Issa
Ahmad Al-Issa le 14 Avr 2024
Modifié(e) : Ahmad Al-Issa le 14 Avr 2024
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 commentaires
Ahmad Al-Issa
Ahmad Al-Issa le 14 Avr 2024
thank you for explanation
Ahmad Al-Issa
Ahmad Al-Issa le 14 Avr 2024
yes you are right. I changed the file name to (data_2020_03_12_08_30_03). there is typos.

Connectez-vous pour commenter.

Réponses (1)

Stephen23
Stephen23 le 14 Avr 2024
Modifié(e) : Stephen23 le 14 Avr 2024
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 commentaires
Stephen23
Stephen23 le 14 Avr 2024
Modifié(e) : Stephen23 le 14 Avr 2024
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 le 14 Avr 2024
Modifié(e) : Ahmad Al-Issa le 14 Avr 2024
thank you, sir,

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by