saving a variable from a .mat file to an excel file
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!