I have not been able to do a lot of blogging on this site of late. Procrastination is the reason. Also, I was a bit busy with my project-work ( or the lack of it!!). Here I write about a small script i wrote to convert from mat files to csv files. I searched a lot for existing solutions but alas none was available though csv to mat converters exit in bulk. While i wrote the script i realized the reason for non existance of such a script.
In Matlab you can store different types of information into a Mat file and some of it might not be easy to represent directly in a CSV file. for example structures cannot logically be put in a CSV file but arrays are easy to convert into csv.
So i wrote a script which can convert mat to csv but with a restriction. The MAT file should not contain any structures or row array's. It can have only column array's. Ofcourse, i know it is simple to change the column to row.
I dont think i can upload a file on to this blog, so till i can put the stuff on esnips this is the only way to share it.:-(
function mat2csv(fname,target)
% MAT2CSV convert a mat file to csv file
% MAT2CSV('matfilename.mat','csvfilename.csv')
% Output is generated using the data provided in the matfile ( one col per data element)
% Unused cells will be filled with zero's
% TODO: Currently the CSV file does not have column names, this will be
% added in next version
if(nargin<2)
error 'Incorrect Number of inputs. Refer to usage by using "help mat2csv"'
end
y = open (fname);
largest_entry =0;
flds = fieldnames(y);
for idx=1:size(flds)
eval ( strcat('if(size(y.',char(flds(idx)),',2)> largest_entry)largest_entry = size(y.',char(flds(idx)),',2); end'));
end
eval ('outmatrix = zeros(largest_entry,size(flds,1));');
for idx=1:size(flds)
eval ( strcat('outmatrix(1:size(y.', char(flds(idx)), ',2),', int2str(idx) ,') = (y.', char(flds(idx)), ');') );
end
fl ='';
for idx=1:size(flds)
fl = strcat(fl,char(flds(idx)),',');
end
[rc,cc] = size(outmatrix);
fid=fopen(target,'wt');
fprintf(fid,'%s\n',fl);
for idx=1:rc
for jdx=1:cc-1
fprintf(fid,'%d,',outmatrix(idx,jdx));
end
fprintf(fid,'%d\n',outmatrix(idx,jdx+1));
end
fclose(fid);
end
In Matlab you can store different types of information into a Mat file and some of it might not be easy to represent directly in a CSV file. for example structures cannot logically be put in a CSV file but arrays are easy to convert into csv.
So i wrote a script which can convert mat to csv but with a restriction. The MAT file should not contain any structures or row array's. It can have only column array's. Ofcourse, i know it is simple to change the column to row.
I dont think i can upload a file on to this blog, so till i can put the stuff on esnips this is the only way to share it.:-(
function mat2csv(fname,target)
% MAT2CSV convert a mat file to csv file
% MAT2CSV('matfilename.mat','csvfilename.csv')
% Output is generated using the data provided in the matfile ( one col per data element)
% Unused cells will be filled with zero's
% TODO: Currently the CSV file does not have column names, this will be
% added in next version
if(nargin<2)
error 'Incorrect Number of inputs. Refer to usage by using "help mat2csv"'
end
y = open (fname);
largest_entry =0;
flds = fieldnames(y);
for idx=1:size(flds)
eval ( strcat('if(size(y.',char(flds(idx)),',2)> largest_entry)largest_entry = size(y.',char(flds(idx)),',2); end'));
end
eval ('outmatrix = zeros(largest_entry,size(flds,1));');
for idx=1:size(flds)
eval ( strcat('outmatrix(1:size(y.', char(flds(idx)), ',2),', int2str(idx) ,') = (y.', char(flds(idx)), ');') );
end
fl ='';
for idx=1:size(flds)
fl = strcat(fl,char(flds(idx)),',');
end
[rc,cc] = size(outmatrix);
fid=fopen(target,'wt');
fprintf(fid,'%s\n',fl);
for idx=1:rc
for jdx=1:cc-1
fprintf(fid,'%d,',outmatrix(idx,jdx));
end
fprintf(fid,'%d\n',outmatrix(idx,jdx+1));
end
fclose(fid);
end
Powered by ScribeFire.