Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

Asked by John on 21 May 2012

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 Comments

John

Tags

Products

No products are associated with this question.

0 Answers

Contact us