Import .txt and save as .mat with conditions

2 次查看(过去 30 天)
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