Accessing data from a file and putting it into a matrix in Matlab, plus headers.
1 view (last 30 days)
Show older comments
I am trying to read data in from a .svc file, which consists of 7 columns and 212 rows. I have managed to read it in by doing:
>>fid = fopen('u001_s01_sign_ds2-tb-0c_01.svc','r');
>>data = textscan(fid,'%f %f %f %f %f %f %f','HeaderLines',1);
>>fclose(fid);
I found it difficult to understand how to read data straight into a matrix, using dlmread, so have tried to go around using that, as above.
To store the data into a matrix after reading it in I used:
>>A=[data{1} data{2} data{3} data{4} data{5} data{6} data{7}]
Now I want to add headers to each column, but I am not sure how to go about that. I have found the following code, that looks like it will do what I need, but I do not understand it:
function writeWithHeader(fname,header,data)
% Write data with headers
% fname: filename
% header: cell of row titles
% data: matrix of data
f = fopen(fname,'w');
%Write the header:
fprintf(f,'%-10s\t',header{1:end-1}); fprintf(f,'%-10s\n',header{end});
%Write the data: for m = 1:size(data,1)
fprintf(f,'%-10.4f\t',data(m,1:end-1));
fprintf(f,'%-10.4f\n',data(m,end));
end
fclose(f);
This code was from http://stackoverflow.com/questions/7081721/adding-a-header-to-a-matrix-in-matlab . The comment to go with this was, ‘You just need to play with the fprintf format string...’. I have saved the code into a function in Matlab, saving it as writeWithHeader.m, and I understand that to run the function I type into the Matlab command window:
>> writeWithHeader('u001_s01_sign_ds2-tb-0c_01.svc', X Y Z A B C D, A)
Where X Y Z A B C D are my header names, and A is the matrix of data I want the headers added to. Is this correct? Is there maybe a better of doing all this?
0 Comments
Accepted Answer
Matt Tearle
on 2 Mar 2012
Is this correct? Basically, yes. When you call the function, header should be a cell array of strings, so
writeWithHeader('u001_s01_sign_ds2-tb-0c_01.svc', {'X' 'Y' 'Z' 'A' 'B' 'C' 'D'}, A)
Also, FWIW, I'd simplify the function to this:
function writeWithHeader(fname,header,data)
% Write data with headers
% fname: filename
% header: cell of row titles
% data: matrix of data
f = fopen(fname,'w');
%Write the header:
n = length(header);
fmt = [repmat('%-10s\t',1,n-1),'%-10s\n'];
fprintf(f,fmt,header{:});
%Write the data:
fmt = [repmat('%-10.4f\t',1,n-1),'%-10.4f\n'];
fprintf(f,fmt,data');
fclose(f);
And you can also simplify your collection of data from textscan by doing either A = [data{:}] or by providing the CollectOutput flag:
data = textscan(fid,'%f %f %f %f %f %f %f','HeaderLines',1,'CollectOutput',true);
A = data{1};
But one question: are you doing anything with the data in A or just writing it back out again? If the latter, here's a function to do it for you
function replaceHeader(filein,fileout,header)
fid = fopen(filein);
x = textscan(fid,'%s');
fclose(fid);
n = length(header);
fmt = [repmat('%-10s\t',1,n-1),'%-10s\n'];
y = [header,x{1}{(n+1):end}];
fid = fopen(fileout,'w');
fprintf(fid,fmt,y{:});
fclose(fid);
Then
replaceHeader('u001_s01_sign_ds2-tb-0c_01.svc','u001_s01_sign_ds2-tb-0c_01.svc', {'X' 'Y' 'Z' 'A' 'B' 'C' 'D'})
(If you were always going to overwrite the file, you could obviously simplify the function to use one filename.)
EDIT TO ADD: Based on the comments, it looks like this should do the job:
function replaceHeader3(filein,fileout,header)
fid = fopen(filein,'r');
data = textscan(fid,'%s','HeaderLines',1);
fclose(fid);
n = length(header);
fmt = [repmat('%-10s\t',1,n-1),'%-10s\n'];
y = [header,data{1}{:}];
fid = fopen(fileout,'w');
fprintf(fid,fmt,y{:});
fclose(fid);
6 Comments
Matt Tearle
on 5 Mar 2012
Ohhhhhh now I get it. I was thinking you had headers (in addition to the "152") and you wanted to strip them off and replace them with something else. I didn't understand that the "152" was the header line you wanted to ignore/replace.
OK, that makes it easy: just change the 'headerlines' from 2 to 1 in the version I posted in my last comment. Or, equivalently, change the (n+1):end to just : in the code you just posted.
I'll add it to my answer, to be complete.
More Answers (0)
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!