Importing data with unequal number of column
Show older comments
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
More Answers (2)
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}]';
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
I have a different interpretation. Each matrix has 178 elements in that text file. There are 3 matrices each segmented by the larger values (eg 10865).
So the task, I believe, is to exract each matrix and convert it to a vector, then concatenate the 3 vcetors into a matrix. That's what my answer does, at least.
Stephen23
on 20 Jan 2020
Adam Danz: wrote: "I have a different interpretation. Each matrix has 178 elements in that text file. There are 3 matrices each segmented by the larger values (eg 10865)."
"So the task, I believe, is to exract each matrix and convert it to a vector, then concatenate the 3 vcetors into a matrix. That's what my answer does, at least."
Your output compared to dpb's:
>> isequal(b(:,2:end),M) % ignore first column of dpb's output
ans =
1
If dpb's answer gives the same output as your (plus one column, as described), what exactly is the problem?
Adam Danz
on 20 Jan 2020
@Stephen Cobeldick,
Both yours and dpb's solutions are much cleaner and more efficient than mine as is stated in my first comment under your answer. The only problem is that the question asks for a 3x178 matrix and these two answers produce a 3x179 matrix. They include unwanted data in the first column. That's the problem. Is there something still unclear?
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.
Adam Danz
on 20 Jan 2020
Agreed, that could be the case.
Categories
Find more on Whos 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!