Reading in a text file with numbers and strings into a cell array

조회 수: 17 (최근 30일)
Akana Juliet
Akana Juliet 2021년 6월 15일
댓글: JJ Lamb 2021년 6월 16일
Hi everyone, I've attached the text file here, and what I'm trying to do is read in line by line and put the strings and numbers into a cell array.
Ideally I would like to read this into a cell array that looks like this:
First Index
0 1 6 2 3
1 3 2 4 0
2 1 6 4 7
3 0 1 4 7
4 0 5 6 1
5 0 1 2 3
...etc
(I just put a bunch of spaces between the numbers but I actually need them in different cells)
(Its okay if the words/strings are all in the same cell because I'm going to try to find a way to skip that anyways)
Can anyone please assist with this? and do you think you can explain how you make it return to the next cell row after 5 columns?
When I try a code like this, I just get all the data in one cell:
t = readtable('index.txt', 'ReadVariableNames', false);
  댓글 수: 2
Stephen23
Stephen23 2021년 6월 16일
편집: Stephen23 2021년 6월 16일
@Akana Juliet: do you have any control over the file format? If those words "First", "Second", etc. were replaced with integers e.g. "Index 1", "Index 2", etc. then this task would probably be a lot easier.
Akana Juliet
Akana Juliet 2021년 6월 16일
@Stephen Cobeldick Yes, I can control the Titles of each category but I can't change the format of the numbers beneath the titles

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

채택된 답변

JJ Lamb
JJ Lamb 2021년 6월 15일
For reading the file in, something like this should work. This will output one cell, but you can index that cell into it's subparts.
fid = fopen('index.txt','r');
t = textscan(fid,'%s','delimiter','\t');
fclose(fid);
You will need to convert the strings to numeric types. Doing what I did below changes the labels like "First Index" to empty values, so you'll want to make sure you save those from the initial t variable.
t2 = t;
for n = 1:length(t2{1})
t2{1}{n} = str2num(t2{1}{n});
end
Last, the easiest way to solve the column problem is being careful with your indexing, if you know how many total columns you have. This following block will take the first row and turn it into the format you want.
reshape(t2{1}{2},5,4)';
First it reshapes the 1x20 row matrix into a 5x4 matrix and then transposes it. Reshape goes down the column first, so you simply transpose once that's done.
Hopefully you can combine these steps to get a result that works for you.
  댓글 수: 4
Akana Juliet
Akana Juliet 2021년 6월 16일
@JJ Lamb Oh I see, thank you for explaining that to me. is there a way to avoid the transpose and just tell the computer 5 columns/values then return, repeat? Because I can't really edit the text file
JJ Lamb
JJ Lamb 2021년 6월 16일
I would check out the documentation for reshape to see if it can do what you want.
Because you have several lines that are mixed in with text, your best option might be to use some combination of a for loop with an if statement something like "if isnum()..." and then append the results together as you go. I don't know if you would be able to do everything in one line.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by