wolffd@0: function m = mirexport(f,varargin) wolffd@0: % mirexport(filename,...) exports statistical information related to wolffd@0: % diverse data into a text file called filename. wolffd@0: % mirexport('Workspace',...) instead directly output the statistical wolffd@0: % information in a structure array saved in the Matlab workspace. wolffd@0: % This structure contains three fields: wolffd@0: % filenames: the name of the original audio files, wolffd@0: % types: the name of the features, wolffd@0: % data: the data. wolffd@0: % The exported data should be related to the same initial audio file wolffd@0: % or the same ordered set of audio files. wolffd@0: % The data listed after the first arguments can be: wolffd@0: % - any feature computed in MIRtoolbox. wolffd@0: % What will be exported is the statistical description of the wolffd@0: % feature (using the mirstat function) wolffd@0: % - any structure array of such features. wolffd@0: % Such as the ouput of the mirstat function. wolffd@0: % - any cell array of such features. wolffd@0: % - the name of a text file. wolffd@0: % The text file is imported with the Matlab importdata command. wolffd@0: % Each line of the file should contains a fixed number of data wolffd@0: % delimited by tabulations. The first line, or 'header', wolffd@0: % indicates the name of each of these columns. wolffd@0: % The file format of the output can be either: wolffd@0: % - a text file. wolffd@0: % It follows the same text file representation as for the input wolffd@0: % text files. The first column of the matrix indicates the name wolffd@0: % of the audio files. The text file can be opened in Matlab, wolffd@0: % or in a spreadsheet program, such as Microsoft Excel, where the wolffd@0: % data matrix can be automatically reconstructed. wolffd@0: % - an attribute-relation file. wolffd@0: % It follows the ARFF standard, used in particular in the WEKA wolffd@0: % data mining environment. wolffd@0: wolffd@0: stored.data = {}; wolffd@0: stored.textdata = {}; wolffd@0: stored.name = {}; wolffd@0: narg = nargin; wolffd@0: if strcmpi(f,'Workspace') wolffd@0: format = 'Workspace'; wolffd@0: elseif length(f)>4 && strcmpi(f(end-4:end),'.arff') wolffd@0: format = 'ARFF'; wolffd@0: else wolffd@0: format = 'Matrix'; wolffd@0: end wolffd@0: v = ver('MIRtoolbox'); wolffd@0: title = ['MIRtoolbox' v.Version]; wolffd@0: class = {}; wolffd@0: if not(isempty(varargin)) && ischar(varargin{end}) && strcmp(varargin{end},'#add') wolffd@0: add = 1; wolffd@0: varargin(end) = []; wolffd@0: narg = narg-1; wolffd@0: else wolffd@0: add = 0; wolffd@0: end wolffd@0: for v = 2:narg wolffd@0: argv = varargin{v-1}; wolffd@0: if isa(argv,'mirdesign') wolffd@0: mirerror('MIREXPORT','You can only export features that have been already evaluated (using mireval).'); wolffd@0: end wolffd@0: if ischar(argv) wolffd@0: if strcmpi(argv,'Matrix') wolffd@0: format = 'Matrix'; wolffd@0: elseif strcmpi(argv,'ARFF') wolffd@0: format = 'ARFF'; wolffd@0: else wolffd@0: imported = importdata(argv,'\t',1); wolffd@0: imported.name = {}; wolffd@0: [stored class] = integrate(stored,imported); wolffd@0: end wolffd@0: elseif isstruct(argv) && isfield(argv,'data') wolffd@0: new.data = argv.data; wolffd@0: new.textdata = argv.fields; wolffd@0: new.name = {}; wolffd@0: [stored class] = integrate(stored,new); wolffd@0: else wolffd@0: new.data = argv; wolffd@0: new.textdata = ''; wolffd@0: new.name = {}; wolffd@0: [stored class] = integrate(stored,new); wolffd@0: end wolffd@0: end wolffd@0: switch format wolffd@0: case 'Matrix' wolffd@0: matrixformat(stored,f,title,add); wolffd@0: m = 1; wolffd@0: case 'ARFF' wolffd@0: classes = {}; wolffd@0: for i = 1:length(class) wolffd@0: if isempty(strcmp(class{i},classes)) || not(max(strcmp(class{i},classes))) wolffd@0: classes{end+1} = class{i}; wolffd@0: end wolffd@0: end wolffd@0: ARFFformat(stored,f,title,class,classes,add); wolffd@0: m = 1; wolffd@0: case 'Workspace' wolffd@0: m = variableformat(stored,f,title); wolffd@0: end wolffd@0: wolffd@0: wolffd@0: wolffd@0: function [stored class] = integrate(stored,new,class) wolffd@0: wolffd@0: if nargin<3 wolffd@0: class = {}; wolffd@0: end wolffd@0: wolffd@0: % Input information wolffd@0: data = new.data; wolffd@0: textdata = new.textdata; wolffd@0: if isfield(new,'name') wolffd@0: name = new.name; wolffd@0: else wolffd@0: name = {}; wolffd@0: end wolffd@0: wolffd@0: % Input information after processing wolffd@0: newdata = {}; wolffd@0: newtextdata = {}; wolffd@0: newname = {}; wolffd@0: wolffd@0: if isstruct(data) wolffd@0: if isfield(data,'Class') wolffd@0: class = data.Class; wolffd@0: data = rmfield(data,'Class'); wolffd@0: end wolffd@0: wolffd@0: if isfield(data,'FileNames') wolffd@0: name = data.FileNames; wolffd@0: data = rmfield(data,'FileNames'); wolffd@0: end wolffd@0: wolffd@0: fields = fieldnames(data); wolffd@0: nfields = length(fields); wolffd@0: wolffd@0: for w = 1:nfields wolffd@0: % Field information wolffd@0: field = fields{w}; wolffd@0: newfield.data = data.(field); wolffd@0: if 1 %not(isnumeric(newfield.data) && all(all(isnan(newfield.data)))) wolffd@0: if isempty(textdata) wolffd@0: newfield.textdata = field; wolffd@0: else wolffd@0: newfield.textdata = strcat(textdata,'_',field); wolffd@0: end wolffd@0: wolffd@0: % Processing of the field wolffd@0: [n class] = integrate({},newfield,class); wolffd@0: wolffd@0: % Concatenation of the results wolffd@0: newdata = {newdata{:} n.data{:}}; wolffd@0: newtextdata = {newtextdata{:} n.textdata{:}}; wolffd@0: newname = checkname(newname,name); wolffd@0: end wolffd@0: end wolffd@0: elseif isa(data,'mirdata') wolffd@0: newinput.data = mirstat(data); wolffd@0: if isfield(newinput.data,'FileNames') wolffd@0: newinput.data = rmfield(newinput.data,'FileNames'); wolffd@0: end wolffd@0: title = get(data,'Title'); wolffd@0: newinput.textdata = [textdata '_' title(find(not(isspace(title))))]; wolffd@0: [n class] = integrate({},newinput,class); wolffd@0: newdata = n.data; wolffd@0: newtextdata = n.textdata; wolffd@0: newname = get(data,'Name'); wolffd@0: elseif iscell(textdata) wolffd@0: % Input comes from importdata wolffd@0: nvar = size(data,2); wolffd@0: newdata = cell(1,nvar); wolffd@0: newtextdata = cell(1,nvar); wolffd@0: for i = 1:nvar wolffd@0: newdata{i} = data(:,i); wolffd@0: newtextdata{i} = textdata{i}; wolffd@0: end wolffd@0: newname = {}; wolffd@0: elseif iscell(data) wolffd@0: for i = 1:length(data) wolffd@0: if not(isempty(data{i})) wolffd@0: % Element information wolffd@0: newelement.data = data{i}; wolffd@0: newelement.textdata = [textdata num2str(i)]; wolffd@0: wolffd@0: % Processing of the element wolffd@0: [n class] = integrate({},newelement,class); wolffd@0: wolffd@0: % Concatenation of the results wolffd@0: newdata = {newdata{:} n.data{:}}; wolffd@0: newtextdata = {newtextdata{:} n.textdata{:}}; wolffd@0: newname = checkname(newname,n.name); wolffd@0: end wolffd@0: end wolffd@0: elseif size(data,4)>1 wolffd@0: % Input is vector wolffd@0: for w = 1:size(data,4) wolffd@0: % Bin information wolffd@0: bin.data = data(:,:,:,w); wolffd@0: if isempty(textdata) wolffd@0: bin.textdata = num2str(w); wolffd@0: else wolffd@0: bin.textdata = strcat(textdata,'_',num2str(w)); wolffd@0: end wolffd@0: wolffd@0: % Processing of the bin wolffd@0: [n class] = integrate({},bin,class); wolffd@0: wolffd@0: % Concatenation of the results wolffd@0: newdata = {newdata{:} n.data{:}}; wolffd@0: newtextdata = {newtextdata{:} n.textdata{:}}; wolffd@0: end wolffd@0: elseif size(data,3)>1 wolffd@0: % Input is vector wolffd@0: for w = 1:size(data,3) wolffd@0: % Bin information wolffd@0: bin.data = data(:,:,w,:); wolffd@0: if isempty(textdata) wolffd@0: bin.textdata = num2str(w); wolffd@0: else wolffd@0: bin.textdata = strcat(textdata,'_',num2str(w)); wolffd@0: end wolffd@0: wolffd@0: % Processing of the bin wolffd@0: [n class] = integrate({},bin,class); wolffd@0: wolffd@0: % Concatenation of the results wolffd@0: newdata = {newdata{:} n.data{:}}; wolffd@0: newtextdata = {newtextdata{:} n.textdata{:}}; wolffd@0: end wolffd@0: elseif size(data,1)>1 && size(data,1)<=50 wolffd@0: % Input is vector wolffd@0: for w = 1:size(data,1) wolffd@0: % Bin information wolffd@0: bin.data = data(w,:,:,:); wolffd@0: if isempty(textdata) wolffd@0: bin.textdata = num2str(w); wolffd@0: else wolffd@0: bin.textdata = strcat(textdata,'_',num2str(w)); wolffd@0: end wolffd@0: wolffd@0: % Processing of the bin wolffd@0: [n class] = integrate({},bin,class); wolffd@0: wolffd@0: % Concatenation of the results wolffd@0: newdata = {newdata{:} n.data{:}}; wolffd@0: newtextdata = {newtextdata{:} n.textdata{:}}; wolffd@0: end wolffd@0: else wolffd@0: if size(data,1)>1 wolffd@0: data = mean(data); wolffd@0: end wolffd@0: newdata = {data}; wolffd@0: newtextdata = {textdata}; wolffd@0: newname = {}; wolffd@0: end wolffd@0: if isempty(stored) wolffd@0: stored.data = newdata; wolffd@0: stored.textdata = newtextdata; wolffd@0: stored.name = newname; wolffd@0: else wolffd@0: stored.data = {stored.data{:} newdata{:}}; wolffd@0: stored.textdata = {stored.textdata{:} newtextdata{:}}; wolffd@0: if isempty(stored.name) wolffd@0: stored.name = newname; wolffd@0: else wolffd@0: stored.name = checkname(newname,stored.name); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: function m = matrixformat(data,filename,title,add) wolffd@0: named = ~isempty(data.name); wolffd@0: if named wolffd@0: if not(add) wolffd@0: m(1,:) = {title,data.textdata{:}}; wolffd@0: end wolffd@0: for i = 1:length(data.name) wolffd@0: m{i+~add,1} = data.name{i}; wolffd@0: end wolffd@0: elseif not(add) wolffd@0: m(1,:) = {data.textdata{:}}; wolffd@0: end wolffd@0: for i = 1:length(data.data) wolffd@0: m((1:length(data.data{i}))+~add,i+named) = num2cell(data.data{i}); wolffd@0: end wolffd@0: if add wolffd@0: fid = fopen(filename,'at'); wolffd@0: else wolffd@0: fid = fopen(filename,'wt'); wolffd@0: end wolffd@0: for i = 1:size(m,1) wolffd@0: for j = 1:size(m,2) wolffd@0: if ischar(m{i,j}) wolffd@0: fprintf(fid,'%s\t', m{i,j}(find(not(m{i,j} == ' ')))); wolffd@0: else wolffd@0: if iscell(m{i,j}) % Problem with key strength pos to be solved wolffd@0: fprintf(fid,'%f\t', m{i,j}{1}); wolffd@0: else wolffd@0: fprintf(fid,'%f\t', m{i,j}); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: %if i < size(m,1) wolffd@0: fprintf(fid,'\n'); wolffd@0: %end wolffd@0: end wolffd@0: fclose(fid); wolffd@0: disp(['Data exported to file ',filename,'.']); wolffd@0: wolffd@0: wolffd@0: function ARFFformat(data,filename,title,class,classes,add) wolffd@0: if add wolffd@0: fid = fopen(filename,'at'); wolffd@0: else wolffd@0: fid = fopen(filename,'wt'); wolffd@0: fprintf(fid,['%% Attribution-Relation File automatically generated using ',title,'\n\n']); wolffd@0: fprintf(fid,'@RELATION %s\n\n',title); wolffd@0: for i = 1:length(data.textdata) wolffd@0: fprintf(fid,'@ATTRIBUTE %s NUMERIC\n',data.textdata{i}); wolffd@0: end wolffd@0: if not(isempty(class)) wolffd@0: fprintf(fid,'@ATTRIBUTE class {'); wolffd@0: for i = 1:length(classes) wolffd@0: if i>1 wolffd@0: fprintf(fid,','); wolffd@0: end wolffd@0: fprintf(fid,'%s',classes{i}); wolffd@0: end wolffd@0: fprintf(fid,'}\n'); wolffd@0: end wolffd@0: fprintf(fid,'\n@DATA\n'); wolffd@0: fid2 = fopen([filename(1:end-5) '.filenames.txt'],'wt'); wolffd@0: for i = 1:length(data.name) wolffd@0: fprintf(fid2,'%s\n',data.name{i}); wolffd@0: end wolffd@0: fclose(fid2); wolffd@0: end wolffd@0: wolffd@0: try wolffd@0: data = cell2mat(data.data(:))'; wolffd@0: catch wolffd@0: error('ERROR IN MIREXPORT: Are you sure all the data to be exported relate to the same ordered list of audio files?'); wolffd@0: end wolffd@0: for i = 1:size(data,1) wolffd@0: fprintf(fid,'%d ',data(i,:)); wolffd@0: if not(isempty(class)) wolffd@0: fprintf(fid,'%s',class{i}); wolffd@0: end wolffd@0: fprintf(fid,'\n'); wolffd@0: end wolffd@0: fclose(fid); wolffd@0: disp(['Data exported to file ',filename,'.']); wolffd@0: wolffd@0: wolffd@0: function m = variableformat(data,filename,title) wolffd@0: m.types = data.textdata; wolffd@0: m.filenames = data.name; wolffd@0: for i = 1:length(data.data) wolffd@0: m.data{i} = data.data{i}; wolffd@0: end wolffd@0: wolffd@0: wolffd@0: function name = checkname(newname,name) wolffd@0: if not(isempty(newname)) && not(isempty(name)) wolffd@0: if length(newname) == length(name) wolffd@0: for i = 1:length(name) wolffd@0: if not(strcmp(name{i},newname{i})) wolffd@0: error('ERROR IN MIREXPORT: All the input are not associated to the same audio files (or the same ordering of these files.'); wolffd@0: end wolffd@0: end wolffd@0: else wolffd@0: error('ERROR IN MIREXPORT: All the input are not associated to the same audio files.'); wolffd@0: end wolffd@0: elseif isempty(name) wolffd@0: name = newname; wolffd@0: end