Import .txt and save as .mat with conditions

조회 수: 2 (최근 30일)
Sanjana Sankar
Sanjana Sankar 2019년 7월 23일
댓글: Sanjana Sankar 2019년 7월 29일
Hello!
I have a .txt file that holds data as follows
c a t
k i: t t e: n
d u: c k
e l e p h a: n t
The words are not of equal length and they are saved with spaces between them, which can be used as a delimiter while reading.
I would like to store this data in .mat format where each cell holds the character(s) as split by the delimiter(' '). I am open to padding at the end of each word(row) in order to make them of equal length (if needed).
Please let me know if this is possible.
  댓글 수: 2
Guillaume
Guillaume 2019년 7월 23일
Typically, if a file has a space between each character it's because you're reading a UTF16 encoded file with a reader that's not aware of UTF16 (e.g. notepad). The spaces are actually 0-value characters.
Can you attach a text file so we know for sure.
Sanjana Sankar
Sanjana Sankar 2019년 7월 29일
No, I've added the spaces for a reason. it is in UTF-8 text file

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 7월 29일
편집: Stephen23 2019년 7월 29일
As you did not supply an example file I created my own (attached).
[fid,msg] = fopen('test.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,'%[^\n]');
fclose(fid);
C = regexp(C{1},'\s+','split');
% Save nested cell vectors:
save('test.mat','C')
% Flatten data and save cell matrix:
L = cellfun('length',C);
N = numel(L);
D = cell(N,max(L));
for k = 1:N
D(k,1:L(k)) = C{k};
end
save('test.mat','-append','D')
And checking:
>> S = load('test.mat')
S =
C: {4x1 cell}
D: {4x8 cell}
>> S.C{:}
ans =
'c' 'a' 't'
ans =
'k' 'i:' 't' 't' 'e:' 'n'
ans =
'd' 'u:' 'c' 'k'
ans =
'e' 'l' 'e' 'p' 'h' 'a:' 'n' 't'
>> S.D
ans =
'c' 'a' 't' [] [] [] [] []
'k' 'i:' 't' 't' 'e:' 'n' [] []
'd' 'u:' 'c' 'k' [] [] [] []
'e' 'l' 'e' 'p' 'h' 'a:' 'n' 't'

추가 답변 (1개)

Kavya Vuriti
Kavya Vuriti 2019년 7월 29일
Hi,
I think you can read data from .txt file and store them in a cell array line by line. Then you can loop through each cell and convert them to ordinary array of underlying data type using cell2mat function. Then try using textscan function to store the character(s) into cells by providing Delimiter as (space).These cells can be stored to a .mat file using save function. Hope this helps.
For more information on the above MATLAB functions, you can refer the following links:
  댓글 수: 1
Sanjana Sankar
Sanjana Sankar 2019년 7월 29일
I have tried this method, the problem is that, I require "u:" in d u: c k to be saved in one cell. But this method considers it as 2 characters and saves it into 2 different cells.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by