Import .txt and save as .mat with conditions

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Guillaume
Guillaume il 23 Lug 2019
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 il 29 Lug 2019
No, I've added the spaces for a reason. it is in UTF-8 text file

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 29 Lug 2019
Modificato: Stephen23 il 29 Lug 2019
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'

Più risposte (1)

Kavya Vuriti
Kavya Vuriti il 29 Lug 2019
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 Commento
Sanjana Sankar
Sanjana Sankar il 29 Lug 2019
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.

Accedi per commentare.

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by