MATLAB Answers

Importing data with unequal number of column

조회 수: 20(최근 30일)
BeeTiaw
BeeTiaw 2020년 1월 20일
댓글: Adam Danz 2020년 1월 20일
Hi all,
Firstly, I apologise if the title is not describing what I am going to ask here. Please feel free to change the title of this post.
I have the following dataset save as txt file and I intend to load this into Matlab. The matrix is suppose to be a [3 x 178], how to do this?
Hit:
The first row is started by '10864.0', the second row is by '10864.5' and the third is by '10865.0'.
I have attached the original data.txt to this post.

채택된 답변

Stephen
Stephen 2020년 1월 20일
편집: Stephen 2020년 1월 20일
This is very simple and efficient using fscanf:
[fid,msg] = fopen('Data.txt','rt');
assert(fid>=3,msg)
mat = fscanf(fid,'%f',[179,3]).';
fclose(fid);
or using fileread and sscanf:
str = fileread('Data.txt');
mat = sscanf(str,'%f',[179,3]).'
Giving:
>> size(mat)
ans =
3 179
>> mat(:,1:11) % Look at just the first few columns:
ans =
10864 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069
10865 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069
10865 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069
  댓글 수: 8
BeeTiaw
BeeTiaw 2020년 1월 20일
I have accepted this answer because it gives me what I want. Thanks!

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

추가 답변(2개)

Adam Danz
Adam Danz 2020년 1월 20일
편집: Adam Danz 2020년 1월 20일
See the other two solutions for more efficient approachs.
The input to the code below is your data file Data.txt. The outputs are 1) M, a 3 x 178 matrix where each row is a block of values from your text file. 2) rowDefs, a 3 x 1 vector identifying the sections of each row of M.
See inline comments for details.
% Read in data a char array, convert to cell array split by rows
Ch = fileread('Data.txt'); % char array
Cs = strsplit(Ch,newline); % Cell array of strings
Cs(cellfun(@isempty,Cs)) = []; % Remove empties
% Convert all elements to numeric
Cv = cellfun(@(c){str2double(strsplit(strtrim(c)))},Cs); % cell array of numeric vectors
% Detect the cell array elements with only 1 value (ie, 10864.0)
sectionIdx = find(cellfun(@numel, Cv) == 1); % numel(sectionIdx) shows that there are 3 sections
% Isolate each section into its own row of a matrix.
M = cell2mat(arrayfun(@(i,j){[Cv{i+1:j-1}]},sectionIdx,[sectionIdx(2:end),numel(Cv)+1])');
% Get row definitions
rowDefs = [Cv{sectionIdx}]';
  댓글 수: 2
Adam Danz
Adam Danz 2020년 1월 20일
Ahh, in that case you did intend for the matrix to be 3x179.
Glad you found an efficient solution!

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


dpb
dpb 2020년 1월 20일
The problem is the file has embedded \n in what should be unbroken records. Whether this came from the original creation of the file or was introduced by looking at in a text editor or what is unknown. If can't fix the problem at the source, then
b=reshape(textread('beedata.txt','%f'),[],3).';
The above presumes the number of records is known a priori and fixed.
I used the deprecated textread because it works for the purpose and doesn't need the extra nuisance of a file handle as do the other input functions.
The above yields for a portion of the file
>> b(:,1:10)
ans =
1.0e+04 *
1.0864 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003
1.0864 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003
1.0865 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003 0.0003
>>
NB: The array size is actually 3x179 since there are 178 elements of the array after each what looks like a time stamp, maybe?
>> whos b
Name Size Bytes Class Attributes
b 3x179 4296 double
>>
  댓글 수: 5
Adam Danz
Adam Danz 2020년 1월 20일
Agreed, that could be the case.

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by