I am working on following code .In the code I am manually loading the matlab file .But know the cases are increases to 27 mat files. can someone help me in automating the process means automatically loading the matlab file from a specific folder and then storing in the excel file .Can someone please help me this . Thank you in advance . Automatically loading means loading the file from the specific folder . without manually loading . For eg in folder ABC I have mat file AAA.mat , BBB.mat , CCC.mat ...So first AAA.mat should load then store the data in excel file (the code that I used) , then BBB.mat & so on ... Thanks in advance
filenames={'AAA.mat','BBB.mat','CCC.mat'}
n = numel(filenames) ;
A = zeros([],n) ;
for i = 1:numel(filenames)
load(filenames{i})
CO= CO_sim_mass_cum;
NOx=NOx_sim_mass_cum;
nmHC= nmHC_sim_mass_cum;
for j = 1:length(CO)
A(j,i) = THC(j);
end
for k= 1:length(NOx)
B(k,i)=NOx(k);
end
for l= 1:length(nmHC)
C(l,i)=nmHC(l);
end
end
xlswrite('e.xlsx',[A,B,C])

 Réponse acceptée

Walter Roberson
Walter Roberson le 2 Fév 2022
dinfo = dir('*.mat');
filenames = {dinfo.name};

3 commentaires

Prasad Joshi
Prasad Joshi le 2 Fév 2022
Thank you sir .. I used the code but not loading in sequence like after C1_H1_C2_case1 should load but instead case19 is loading any inputs how can i load in sequence I also tried to sort the file but no use the same sequence it is showing .. Thank you in advance
Prasad Joshi
Prasad Joshi le 3 Fév 2022
dinfo = dir('*.mat');
filenames = {dinfo.name};
filenames_sort=natsortfiles(filenames)
n = numel(filenames_sort) ;
A = zeros([],n) ;
for i=1:numel(filenames_sort)
load(char(filenames_sort(i)))
end
detail code if anyone needed .. Thank you Walter & Yongjian san for your support
Prasad Joshi
Prasad Joshi le 9 Fév 2022

Connectez-vous pour commenter.

Plus de réponses (1)

Yongjian Feng
Yongjian Feng le 1 Fév 2022
For "automatically loading", you most likely mean generating the file names in a loop?
Try this:
num_case = 26;
files={};
for i=1:num_case
filename = ['C1_H1_C2_case' num2str(i) '_WLTC_']; % something else at the end? Not shown in the attached screenshot
files{end+1} = filename;
end

7 commentaires

Prasad Joshi
Prasad Joshi le 2 Fév 2022
No san automatically loading means loading the file from the specific folder not generating the file . I have the MAT file I just wanted to load the files. Without manually loading all the cases. Thank you for the code .
Yongjian Feng
Yongjian Feng le 2 Fév 2022
This code only creates the file names, not the files.
Then you can loop through the file names to load the file.
Or you can follow Walter's answer below. It will find all the *.mat file in the folder. The code above only finds the files that starts with C1_H1_C2_case.
Prasad Joshi
Prasad Joshi le 2 Fév 2022
Thank you san .. I used the code but not loading in sequence like after C1_H1_C2_case1 should load but instead case19 is loading any inputs how can i load in sequence I also tried to sort the file but no use the same sequence it is showing .. Thank you in advance
Walter Roberson
Walter Roberson le 2 Fév 2022
See the File Exchange for natsortfile
Yongjian Feng
Yongjian Feng le 2 Fév 2022
This code shall handle it right. This code gives you the flexibility to set the sequence.
Prasad Joshi
Prasad Joshi le 3 Fév 2022
Thank you san for your support
Prasad Joshi
Prasad Joshi le 9 Fév 2022
yongjian san can you answer the below question thank you in advance .. it would be higly helpful

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by