How can I convert file txt into matrix?

5 views (last 30 days)
Mira le
Mira le on 15 Dec 2021
Answered: dpb on 15 Dec 2021
Hello every one,
I have file text contain
10307 10311 12487
12559
12695 12703 18715
10311 12387 12515 12691 12695 12699 12703 12823 12831 12847 18595 18679 18751
10291 12523 12531 12535 12883
12523 12539 12803 12819
12819
12471 12491 12531 12535 12567 12663 12667 12823 18447 18507 18691
12487
I read this file
fid = fopen('D:\PhD Computer science 2019\Implementation\Disaggregate approach\k-anonymity\bms1\bms1.txt'); %%D:\PhD Computer science 2019\Implementation\Final version\datasets\chess\chess.txt
lines = textscan(fid,'%s','delimiter','\n');
lines = lines{1}; % flatten the cell array
fclose(fid);
%convert each row into a nested cell array of numbers
transactions = {[]};%cell(size(lines));
for i = 1:length(lines)
line = textscan(lines{i},'%d');
transactions{i} = line{1}';
end
T=transactions;
T=T';
I put what in file text in T is a cell array
I use cell2mat to convert T, but an error appear, beause the dimension of T is not fixed
matr=cell2mat(T);
How can I do that?

Answers (1)

dpb
dpb on 15 Dec 2021
Simply put, you can't. An array of anything (even a cell array) has to be regular (your cell array above is Nx1; it is only the content of the various cells that have a different length by alement).
You can only augment the shorter rows with NaN or other values to the length of the longest record in order to save the input file data shape and have a double or other numeric array.
If you would provide some indication of what you need/want to do with the result, folks could probably give you hints on how to write code to efficiently handle your data.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!