extracting data from text file

I have a text file which consists of only numbers in the form of an array. I wish to create a for loop which will extract first 100 digits for the first loop, next 100 digits for the second loop. Could anyone help me please?

댓글 수: 5

Stephen23
Stephen23 2017년 6월 22일
편집: Stephen23 2017년 6월 22일
It would likely be faster and more efficient to load the file into memory, and perform any looping over the array in memory. How big is the array?
shru s
shru s 2017년 6월 22일
hello, my matrix is quite big, it is 27x34992. i have 27 rows with 34992 elements in each. If I could exctract one row at a time also, it will be brilliant. thank you
shru s
shru s 2017년 6월 22일
편집: shru s 2017년 6월 22일
would i be able to do this if my data was in an excel file? edit: nope not possible as my data is more than 1024 in length
alice
alice 2017년 6월 22일
편집: alice 2017년 6월 22일
You can do it in a simple way opening the file with fopen, reading all you want to read with textscan and then closing it with fclose. See doc of textscan: www.mathworks.com/help/matlab/ref/textscan.html
Stephen23
Stephen23 2017년 6월 22일
편집: Stephen23 2017년 6월 22일
Using textscan would require constructing a rather large format string. If all of the data is numeric then much simpler would be to use csvread or dlmread. Looping one line-at-a-time would not be very efficient.

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

답변 (1개)

dpb
dpb 2017년 6월 22일
편집: dpb 2017년 6월 22일

0 개 추천

>> 27*3500*8/1024/1024
ans =
0.7210 MB
>>
Isn't all that big by today's standards. Just read the file and do whatever as Stephen suggested.
x=textread('yourfile','');
For the base case; adjust for delimiter, etc., as required if required (we aren't given those details).
textread works for such simple files easier than textscan as it
  1. Uses the filename rather than needing fopen and file handle,
  2. returns double array be default where a cell array is unneeded overhead
  3. the empty format string(*) returns the array in the shape as in the file as number of fields per record so don't have to count delimiters or know the number a priori.
() In fairness, this is a feature in |*textscan|, too, although the documentation of it while extremely valuable is getting harder to find with each and every release it seems.

질문:

2017년 6월 22일

편집:

dpb
2017년 6월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by