필터 지우기
필터 지우기

How to load texfile with lots of emptydata values?

조회 수: 1 (최근 30일)
Cameron Spooner
Cameron Spooner 2016년 8월 14일
편집: Stephen23 2016년 8월 14일
I have a tab delineated textfile which is formatted as below;
1 2 3 4 5 6 7 8
1 * * * 5 * * *
1 * * * * * * *
1 * 3 * * * * *
1 * * * * * * 8
The first row of data is headers which I am not loading. I have used a * above to represent an emptydata value in the text file. The first column has continuous values but the rest are mostly made up of empty values.
I have tried using textscan to load the data but it only seems to load the first column and ignore the others even though I have tried to set it up to deal with empty values. The code I am using for this is;
indata = textscan(fid, '%s%s%s%s%s%s%s%s', 'Delimiter', '\t', 'HeaderLines',1, 'EmptyValue',0);
fclose(fid);
data = indata{1};
The desired output I am looking for is;
1 2 3 4 5 6 7 8
1 0 0 0 5 0 0 0
1 0 0 0 0 0 0 0
1 0 3 0 0 0 0 0
1 0 0 0 0 0 0 8
What do I need to change to the code above to get it to do this?
  댓글 수: 2
Stephen23
Stephen23 2016년 8월 14일
@Cameron Spooner: please edit your question and upload a sample file by clicking the paperclip icon that you will find above the textbox.
Cameron Spooner
Cameron Spooner 2016년 8월 14일
Attached the file

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

채택된 답변

Stephen23
Stephen23 2016년 8월 14일
편집: Stephen23 2016년 8월 14일
This reads the file (attached):
opt = {'Delimiter','\t', 'HeaderLines',1};
fmt = repmat('%s',1,8);
fid = fopen('Rockfalls.txt');
C = textscan(fid,fmt,opt{:});
fclose(fid);
C = horzcat(C{:});
And (part of) the output:
>> C(1:4,:)
ans =
[1x20 char] '' '' '' '' 'X' '' ''
[1x20 char] '' '' '' '' '' '' ''
[1x20 char] '' '' '' '' '' '' ''
[1x20 char] '' 'X' '' '' '' '' ''
When you read the textscan documentation it clearly says that the option 'EmptyValue' only applies to numeric fields: "Value to return for empty numeric fields in delimited files", so there is no point is using this with your string data.
If you want to replace the empty cells of the cell array, then try this:
C(cellfun('isempty',C)) = {0};
to give:
>> C(1:4,:)
ans =
[1x20 char] [0] [0] [0] [0] 'X' [0] [0]
[1x20 char] [0] [0] [0] [0] [0] [0] [0]
[1x20 char] [0] [0] [0] [0] [0] [0] [0]
[1x20 char] [0] 'X' [0] [0] [0] [0] [0]
  댓글 수: 1
Cameron Spooner
Cameron Spooner 2016년 8월 14일
편집: Stephen23 2016년 8월 14일
Thanks that works perfectly!!!

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

추가 답변 (0개)

카테고리

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