how to extract part of the information?

This is what i have. (note: originally i have 7 columns but i have only shown just 2 columns for convenience)
'No.' 'Time'
'1' '0.000000000'
'2' '0.100209000'
'3' '0.199458000'
'4' '0.299589000'
'5' '0.399856000'
'6' '0.499050000'
'7' '0.599287000'
'8' '0.698449000'
'9' '0.798677000'
'10' '0.898885000'
'
'1000' '99.00000000'
Here is the code i use to extract
fid = fopen('eT1.txt', 'r');
data = textscan(fid, '%d%f%s%s%s%d%s', 'MultipleDelimsAsOne',true, 'HeaderLines',1);
fclose(fid);
But what i want only 1000 values extracted. In that case i need all the 7 columns but till No==1000. how to do that?

 채택된 답변

Guillaume
Guillaume 2015년 1월 23일

0 개 추천

Assuming you only want 1000 rows and not just 1000 values (which is not a multiple of 7):
data = textscan(fid, '%d%f%s%s%s%d%s', 1000, 'MultipleDelimsAsOne',true, 'HeaderLines',1);
It's all there in the documentation of textscan (parameter N)

댓글 수: 1

Chathu
Chathu 2015년 1월 23일
@ Guillaume- actually i even typed the very same thing as your code except i placed the value '1000' after true.That's why it didn't work.
Your code works beautifully. Thanks alot.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 1월 23일
편집: Azzi Abdelmalek 2015년 1월 23일

0 개 추천

n=1000
data=cell(n,1);
fid = fopen('file.txt', 'r');
fgetl(fid);
for k=1:n
data{k} = fgetl(fid);
end
fclose(fid);
celldisp(data)

댓글 수: 1

@ Azzi- thank you for your response.
Can you kindly tell me, what does '1' refers in the below line:
data=cell(n,1);
Here is what i obtain:
ans{1} =
1 0.000000000 192.168.20.30 192.168.20.2 UDP 60 03ff00000166
but i want to obtain everything in separate columns(as shown below) with their respective headers. How to obtain that?
No Time
'1' '0.000000000'
'2' '0.100209000'

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2015년 1월 23일

0 개 추천

You forgot to attach your file. Are the single quotes actually in your file or not? Whynot just use a table:
t = readtable(filename);

댓글 수: 3

Chathu
Chathu 2015년 1월 23일
@ Image analyst: thank you so much for your response.
Actually original file is a .txt extension. Hence, it does not have quotes. Quotation came when i import the values to MatLab. (sorry if i confuse you)
I cannot use 'readtable' because original file has alphanumeric values as well.
Image Analyst
Image Analyst 2015년 1월 23일
readtable() takes alphanumeric values. It should be the easiest way, though it requires R2013b or later. You could speed things up (the back and forth question asking) if you would just attach the file, at least just a few lines of it.
Chathu
Chathu 2015년 1월 23일
@ Image Analyst- alright. Thanks for your response.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2015년 1월 23일

댓글:

2015년 1월 23일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by