Hi,
Based on my understanding of the issue you described. I would suggest a solution. Consider the points below.
>> Z = readtable(infile)
>> A = Z{:,:}
The line of code above, loads a table into ‘Z’ variable. The table stores data along with respective column names (or headers). But ‘A’ variable stores a double array extracted from the table ‘Z’, due this extraction, the column names/headers are lost.
>> outdata = (A - mean(A))./ X
‘outdata’ is also a double array(or matrix) . So, after saving it, the headers are not included in the excel file.
The solution to this problem is to convert the ‘outdata’ matrix to a table ‘outtable’ with headers’ same as that of the ‘Z’ table.
The headers of ‘Z’ table are obtained through:
>>Z.Properties.VariableNames
This is done through following code:
outtable = array2table(outdata,'VariableNames',Z.Properties.VariableNames)
writetable(outtable,outfile)
The complete solution code is written below:
SOLUTION:
mat = readtable('infile.xlsx')
A = mat{:,:}
X = std(A)
outdata = (A - mean(A)) ./ X
outtable = array2table(outdata,'VariableNames',mat.Properties.VariableNames)
writetable(outtable,outfile)
indir = 'C:\Users\Maki\Desktop\PoissonWeek\ForScale';
outdir = 'Scaled';
if ~exist(outdir, 'dir'); mkdir(outdir); end
dinfo = dir(fullfile(indir, '*.xlsx'));
filenames = {dinfo.name};
nfiles = length(filenames);
for K = 1 : nfiles
thisfile = filenames{K};
infile = fullfile(indir, thisfile);
outfile = fullfile(outdir, thisfile);
Z = readtable(infile)
A = Z{:,:}
X = std(A)
outdata = (A - mean(A)) ./ X
outtable = array2table(outdata,'VariableNames',Z.Properties.VariableNames)
writetable(outtable,outfile)
disp('An error occurred while retrieving information from the internet.');
disp('Execution will continue.');
end
Hope this helps.
Best Answer