Importing data with unequal number of column

24 ビュー (過去 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