reading strings between spaces from a text file.

조회 수: 25 (최근 30일)
Rakesh Praveen
Rakesh Praveen 2014년 9월 28일
댓글: Geoff Hayes 2021년 10월 15일
The text file 'test_text.txt' contains something like this:
2.000000e-01 3 4.230000e-0 1 2
I wanted to pick each number between the spaces in a array and later i can process them. I am able to pick what I need with the code below. So if you see in line2 'X_data' contains all strings in a array so i can access them later. But say suppose there are 100 numbers in that line in the text file and i wanted to pick X_data(90) then how do I do it without keep writing %s so many times in textscan command ? Thank you.
fid = fopen('test_text.txt','r');
X_data = textscan(fid2,'%s %s %s %s');
str = X_data(4);
fclose('fid');

채택된 답변

Mohammad Abouali
Mohammad Abouali 2014년 9월 28일
you could use
fid = fopen('test_text.txt','r');
xdata=str2num(fgets(fid))
fclose(fid)
or you can use
M = dlmread(filename, delimiter)
or you can use
T = readtable(filename)
  댓글 수: 2
Rakesh Praveen
Rakesh Praveen 2014년 9월 28일
편집: Rakesh Praveen 2014년 9월 28일
i don't know why when i use
X_data = readtable('test_text.txt');
i get: Undefined function 'readtable' for input arguments of type 'char'. BTW it looks like i have do str2num like your first suggestion and then reverse back for my purpose.
Mohammad Abouali
Mohammad Abouali 2014년 9월 29일
what version of matlab do you use? May be in your version that function is not yet included. Not sure when readtable was added to matlab.

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

추가 답변 (2개)

Geoff Hayes
Geoff Hayes 2014년 9월 28일
Rakesh - is it necessary that you treat all the data in the file as strings, especially since they appear (from your example) to be numeric? If not, then you could import the data using importdata where you specify that the delimiter between each column of data is the space character
X_data=importdata('test_text.txt',' ');
Then X_data(90) would return the 90th element from the only row in that file.
  댓글 수: 2
Rakesh Praveen
Rakesh Praveen 2014년 9월 28일
when u read from a textfile they are in string format right? But, Yes i want them to store as a string in a array. But when i use 'importdata', X_data contains only 1*1 cell with one single string and now i can't do X_data(1,2). Even if i can read them in numeric how would i put them in a array avoiding the spaces ?
Geoff Hayes
Geoff Hayes 2014년 9월 28일
Did you set the delimiter type (second input to importdata) to be the space character? I took your example and pasted it into a file, and was able to read in the data using the above line of code.

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


Mohammad R. Ghassemi
Mohammad R. Ghassemi 2021년 10월 14일
Hi,
I need to read each line in a text file as a comple string including space such as 'n30e 45se', and apply a set of functions such as 'M=strfind(str,'n')' to each line, and get an output text file.
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2021년 10월 15일
@Mohammad R. Ghassemi - as this isn't an answer to the question, please delete it and post it as a new question.

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

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by