Separating a colymn into two columns (.txt file)
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello,
I would like to separate a column into two different columns from a .txt file in Matlab. I used these commands:
t=readtable('file.txt','Delimiter','\t','HeaderLines',1)
and the results of these commands are in the pictures. Finally, I mean that i would like to separate line 11 into two vertical columns.
Which command should I use?
채택된 답변
I would do it like this:
%t = your table;
dataCell = table2cell(t); % convert to cells
dataCell = dataCell(11:end); % remove unneccessary text
cellNums = cellfun(@str2num,dataCell,'UniformOutput',0); % apply str to num on all the data
output = cell2mat(cellNums); % convert to matrix with 2 columns
*Edit: fixed a few things
댓글 수: 10
I used these commands and command window shows:
Error using cellfun
Input #2 expected to be a cell array, was char instead.
What should I do?
Error in line 17
cellNums = cellfun(@str2num,dataCell,'UniformOutput',0); % apply str to num on all the
data
hm, that is unexpected. Could you post your actual data as like a mat file or something so I can more thoroughly test what my answer?
Well, my data are a .txt file which has this format:

Alright, I fixed my answer it should work now.
It works ,Thanks a lot !!!
Excuse me but I have one more question,
If I had a .txt file (see image) like this and I would like to separate it into columns, like the previous one, which command should I use ?
I believe that the problem is in command
output = cell2mat(cellNums); % convert to matrix with 2 columns
(I mean I would like to separate my file in 8 columns).

One issue I see is that minY and maxY are only on 1 row so it creates issues with there being differently sized cells that cannot be converted to a matrix.
To fix that I would pad the cells so that they are all the same size. I am going to do that by padding each cell with zeros.
cellNums = cellfun(@(x)[x,zeros(1,8-length(x))],cellNums,'UniformOutput',0)
This will make it so that every cell has 8 columns and can then be put into the cell2mat function.
Ok, one last question please. If i would want to "pick" from this .txt file (see image up) only the number of 9th line which command would help me to make it?
Personally, I'm not great at reading in and out files all that well. I think there are some commands that let you set where you are reading in from and how how much to read in. But since the matrix is all ready read into matlab I would just use the matrix index to choose the value you need.
If you really only want to get one value then I would check out fseek and other f- commands.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
