saving a variable from a .mat file to an excel file

1 view (last 30 days)
Hello,
The code below loops through .mat files and writes the variable 'ess_plant_energy_in_total_simu' in a mat file to an excel file. It names the excel files file1, file2 etc.
Could anybody tell me how I could extend it to write another variable called 'sch_cycle' to the same excel file also.
This variable has 2 columns of data.
I'd appreciate any help - I'm having no luck.
John
filetofind = 'data.mat';
dirinfo = dir();
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
tf = ismember( {dirinfo.name}, {'.', '..'});
dirinfo(tf) = []; %remove current and parent directory.
numsubdir = length(dirinfo);
lastmod = inf * ones(numsubdir,1);
for K = 1 : numsubdir
subdirinfo = dir(fullfile(dirinfo(K).name, filetofind));
if ~isempty(subdirinfo)
lastmod(K) = subdirinfo(1).datenum;
end
end
[sortedmod, sortorder] = sort(lastmod);
sordorder(~isfinite(sortedmod)) = []; %directories without data.mat
vartofind = 'ess_plant_energy_in_total_simu';
for K = 1 : length(sortorder)
thisdirnum = sortorder(K);
thisdirname = dirinfo(thisdirnum).name;
thisoutname = sprintf('file%d.xls', K);
try
thisdata = load( fullfile(thisdirname,filetofind), vartofind );
xlswrite( thisoutname, thisdata.(vartofind) );
catch
fprintf(2, 'File "%s/%s" does not have variable "%s\n"', thisdirname, filetofind, vartofind);
end
end

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!