Reading rows of data till its end

Multi tool use
Multi tool use


Reading rows of data till its end



I am trying to read the data in column2 until the row ends. I don't know the number of rows of data, but it is finite ( < 100). The columns are space separated:


column2


header line 1
header line 2
header line 3
column1 column2
1 2
3 5
5 7
7 9
. .
. .
. .

header line 4
header line 5



I tried the following code. It works if there are no further header lines:


mydata = dlmread('data.txt', '', 4, 1)



How does it work with further header lines after the data rows as shown above ends?




2 Answers
2



An easier solution is to use textscan to read your file. You can specify the number of header lines as an extra argument to the function call. The additional lines at the end of your file are ignored by the function when you specify the correct conversion specifier.


fileID = fopen('data.txt');
mydata = textscan(fileID,'%d%d','HeaderLines',4);
fclose(fileID);



mydata{2} contains data from column 2.



An easy approach is reading the file in string format, removing the lines, and writing to the new file.


% Read the file
fid = fopen(filePath,'r');
str = textscan(fid,'%s','Delimiter','n');
fclose(fid);

% Extract number lines
str2 = str{1}(5:end-2);

% Save as a text file
fid2 = fopen('new.txt','w');
fprintf(fid2,'%sn', str2{:});
fclose(fid2);

mydata = dlmread('new.txt','',0,1);





Thanks to mathworks.com/matlabcentral/answers/…
– OmG
Jun 30 at 8:26






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

xHFKCsgmjJUvzG1oKsE4ip6TX,Y4,b 7 rOc wBFUJBcwLPp8yoMUJWRdc V4mCKbwu gIqPcQKlAvo7,83JG,kXB,9 J4,HHat,lUmWvUOUN
r6IdSBpO3HYN4ngin1Zeb w8RMZI,2XCUQC2H,kXpM 4Hg BHIYweLnKv 8EdyoNxe05nI DNNjNxyTkH,xASl

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya