Importing data with unequal number of column

17 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 20 Jan 2020
Edited: Stephen23 on 20 Jan 2020
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 Comments
Adam Danz
Adam Danz on 20 Jan 2020
"and yet you have failed to explain or justify what this means"
I did expland that cricisms 2 comments later.
"it is trivial to ignore/remove that column if required"
Agreed.
"Are you now implying that my code does isolate those "sections","
Yes, provided the first column is removed, or, as dpb mentioned, the OP incorrectly defined the problem in the quesiton.
BeeTiaw
BeeTiaw on 20 Jan 2020
I have accepted this answer because it gives me what I want. Thanks!

Sign in to comment.

More Answers (2)

Adam Danz
Adam Danz on 20 Jan 2020
Edited: Adam Danz on 20 Jan 2020
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 Comments
BeeTiaw
BeeTiaw on 20 Jan 2020
This answer works fine too if I add one extra line to merge the matrices.
out = [rowDefs M];
Adam Danz
Adam Danz on 20 Jan 2020
Ahh, in that case you did intend for the matrix to be 3x179.
Glad you found an efficient solution!

Sign in to comment.


dpb
dpb on 20 Jan 2020
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 Comments
dpb
dpb on 20 Jan 2020
Nothing's not clear, no. The difference of opinion is the OP in SC's and mine interpretation of the request is to augment the 178 elements with the section ID number resulting in a [3x1 3x178] --> [3x179] output array total.
If OP doesn't really want the first column, it's trivial to elide it after the fact, but I'm betting it's wanted data, too, and simply didn't account for it in the original Q? text.

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!