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 件のコメント
Sanjana Sankar
Sanjana Sankar 2019 年 7 月 29 日
This is perfect! Thanks a lot!

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by