필터 지우기
필터 지우기

Delimiting a text file like "Text to columns" and viewing the output

조회 수: 2 (최근 30일)
Hello, I am trying to import data from a Text file (.txt). This data has a single column of mixed (numeric and string) data that is | (bar) delimited. My goal is to import this data and seperate each delimited portion into a seperate columns while maintaining the rows. Essentially, I am trying to do the Excel "Text to columns" with MATLAB. Thank you
Below is a step-by-step output:
%open file
fid = fopen(YourFile,'rt')
fid =
5
%figure out how many columns are there
firstline = fgetl(fid)
firstline =
RMS Functional Block|Reference Designator|Part Number|Sheet Number|Description|Invisible
ncol = 1 + sum(firstline == '|')
ncol =
6
%reset to beginning of file
fseek(fid,0,0)
ans =
0
%read data
indata = textscan(fid,repmat('%s',1,ncol),'Delimiter','|','CollectOutput',1)
indata = {646x6 cell}
%close file
fclose(fid)
ans =
0
How do I look at indata's data?
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2011년 9월 15일
If it's 1x1 cell array, do this to
NewInData=indata{1} to get its element which is 646x6 cell array, which is what you really want. Then use the method in my answer below.
Also, try not to use the ('CollectOuuput',1) option. I think it will give you directly the 646x1 cell array.
Walter Roberson
Walter Roberson 2011년 9월 15일
CollectOutput is correct here, Fangjun. Without it you would get a 646x6 cell array in which each entry was a cell array containing a string; e.g., {{'a'},{'b'}} rather than the present {'a','b'}

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 15일
The data is a cell array. You can:
1. type indata{1,1} to see the first element
2. use celldisp(indata) to see all
3. use open('indata') to see all
  댓글 수: 6
Brian
Brian 2011년 9월 15일
Yes, how can I place a number produced by an equation into a cell array?
Fangjun Jiang
Fangjun Jiang 2011년 9월 15일
A={'a','b';1 2};
V=3+4;
A{2,1}=V

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 9월 15일
indata{L,C} will be the string that was on line #L at column #C
For example, indata{3,7} is column 7 of line 3.

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by