How can I convert file txt into matrix?

조회 수: 2(최근 30일)
Mira le
Mira le 2021년 12월 15일
답변: dpb 2021년 12월 15일
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?

답변(1개)

dpb
dpb 2021년 12월 15일
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.

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by