How can I export a dataset with an extra line before the header in the Statistics Toolbox 7.4 (R2010b) ?

1 view (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Aug 2011
The ability to add headers while exporting a dataset is not available in the Statistics Toolbox 7.4 (R2010b).
There are two workarounds.
The first workaround is to customize your code so as to add headers. You can refer to following code snippet which perform this task. In this example, the input file is a text file with three columns, separated by commas. The first row of this input file is the header and the other rows are contents. Please download the attached file "inputDataSet.csv" and execute the following:
inputFileName='inputDataSet.csv';
outputFileName='outputDataSet.csv';
extraLine='extraLine';
% read in the dataset from the input file
dataSet=dataset('file', inputFileName, 'Delimiter', ',');
% output the extra line as the first row in the output file
outputFileHandle=fopen(outputFileName, 'w');
fprintf(outputFileHandle, '%s\r\n', extraLine);
% print the header to the output file
for i=1:numel(dataSet.Properties.VarNames)
fprintf(outputFileHandle, '%s,', dataSet.Properties.VarNames{i});
end
fprintf(outputFileHandle, '\r\n');
% print the dataset to the output file
numbers=size(dataSet);
numRows=numbers(1);
numCols=numbers(2);
for i=1:numRows
for j=1:numCols
if j ~= 1
fprintf(outFileHandle, ',');
end
varFormat=getFormat(dataSet{i,j});
fprintf(outFileHandle, varFormat, dataSet{i,j});
end
fprintf(outFileHandle, '\r\n');
end
% This function is used to produce the format for the data
function varFormat=getFormat(data)
varType=class(data);
switch varType
case 'double'
varFormat='%f';
case 'char'
varFormat='%s';
otherwise
varFormat='%s';
end
Please note that the above workaround is for the given input file and one may need to customize this workaround for datasets with different format.
The second workaruond applies if you plan to make exporting extra information to the header as the default behavior of the EXPORT function. In this regard, you can use the 'Description' property of a dataset and add it to the exported dataset. You need to launch MATLAB as administrator in order to conduct the change.
The 'Description' is a string that contains a description of the dataset. For instance, if your dataset object in
MATLAB was called 'sampleDataset', then its description could be set as follows:
sampleDataset = set(sampleDataset,'Description','This is my description');
To retrieve the description of a dataset, you can use the GET method:
get(sampleDataset,'Description');
Note that the property name is case sensitive. Any relevant information - like statistics, experimental conditions etc. - can be added to the description.
Unfortunately, this description is not exported into the text file when you use the EXPORT function with a dataset. To workaround this issue, you can modify the code of the EXPORT function to do so. This can be
done in the following steps:
1. Open export.m for editing. To ensure that you edit the correct export.m file, execute the following
commands:
file = fullfile(matlabroot, '\toolbox\shared\statslib\@dataset\export.m');
oldfile= fullfile(matlabroot, '\toolbox\shared\statslib\@dataset\export-old.m');
copyfile(file, oldfile);
edit(file)
2. Insert the following line of code before line 191, the place where the variable names are written to the text
file:
fprintf(fid,[get(a,Description),\n]);
3. Save the file, and close it.
4. Export the dataset now, using the following command:
export(sampleDataset);
Your description will be available as the very first line in the resultant text file. Please note that this makes a change to our built in file, the resulting behavior has not been fully tested on our end.

More Answers (0)

Products


Release

R2010b

Community Treasure Hunt

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

Start Hunting!