Matlab: read only header line from a .csv-file
Assume there's a myfile.csv with variable names in the first row and decimal numbers in the following ones. In Matlab I'd like to read the header line and the decimal numbers separately. So far, I've been doing the following to extract the header line:
fid = fopen('myfile.csv'); a = textscan(fid,'%s','Delimiter','\n'); b = a{1,1}; fclose(fid); c = textscan(b,'%s','Delimiter',','); d = c{1}
Then, I use the csvread command to extract the numerical part of the file. But there should be a (much) easier way to do it! First, I don't want to read the whole file (as with a = textscan(fid,'%s','Delimiter','\n');
) to extract only the first line. Second, it looks wrong to use 7 lines of code to do it - can it be done with less?
I'd be thankful for any constructive suggestions.
NOTE:-
Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.Answers:Open the file with
fopen
, read the header line withtextscan
, read the decimal numbers withfscanf
, and callfclose
in the end - only 4 lines in total :) Example input file:
Weight,Size,Count 1,2,3 4,5,6 7,8,9 10,11,12Reading of this file:
SEE COMPLETE ANSWER CLICK THE LINKhttps://matlabhelpers.com/questions/matlab-read-only-header-line-from-a-csv-file.php
Comments
Post a Comment