Import .txt and save as .mat with conditions
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sanjana Sankar
el 23 de Jul. de 2019
Comentada: Sanjana Sankar
el 29 de Jul. de 2019
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 comentarios
Guillaume
el 23 de Jul. de 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.
Respuesta aceptada
Stephen23
el 29 de Jul. de 2019
Editada: Stephen23
el 29 de Jul. de 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'
Más respuestas (1)
Kavya Vuriti
el 29 de Jul. de 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:
Ver también
Categorías
Más información sobre Data Import and Export en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!