how can i select from an xls header with multiple rows the right row for column names?
古いコメントを表示
hey all, i use the same code like the one in the tutorial video of Doug Hull see Mathworks . This code works perfectly only, if the header of the xls file has only one row (string). In my case the header of my xls file has multiple rows with string n numbers see below.

How can i select the header row and ignore (or delete) the other rows (row1-to-row7)? in this case i want row 8 as a header.... but this position varies from file to file, it's not always row 8... I tried for example with
if
array = importdata(fileName,' ',7) %to import data n textdata then
b=array.textdata(end,:) % select the last row in textdata
array1 = xlsread( fileName );
v = genvarname(b) % generate a Variable name from b
end
and then I tried to merge b or v (<1x3 cell>) with array1 (<17 x3 double>)... etc. no chance! Who can help? thank you in advance!
回答 (1 件)
Unless there's some klew a priori (like from the name of the file, say) to let you know how many header lines there are, then the only choice I would see would be to open the file and fgetl lines and search for whatever is the particular header...hmmm...for such a file textscan has the 'commentstyle' named parameter combined with 'headerline' that would work the cat's meow it would seem if you don't want/need the header line--or, my personal favorite textread as it returns an array instead of fooling with cell arrays and opens the file directly instead of needing fopen/fclose.
dat=textread('yourfile','','commentstyle','shell','headerlines',1);
If you do need the header row and there is always at least one comment line, then you can count those to find the number and revert to the previous with something like
n=0; % initialize counter
l=fgetl(fid); % read first comment line
while l(1)=='#' % until run out of comments
n=n+1; % increment counter first for previous read
l=fgetl(fid); % get next line...
end
hdrline=l; % the last line read will be the header line
n will be the number of comment lines; the header line will be the n+1 st. Now you can either continue reading from here with one of the low level routines or fclose and use xlsread as before with the known count (altho it would seem to make little sense for the latter having now already parsed everything except the easy numeric array).
カテゴリ
ヘルプ センター および File Exchange で Large Files and Big Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!