How do I save a cell array that has both text and numeric data?
Mostrar comentarios más antiguos
I have a data set where I would like to extract alphanumeric data to a text file. E.g. here is one 1x6 cell array:
37 0 240 0.436 'cat dog mouse' 10
I would like to save this data into a text file but I cannot do that with the fprintf command since it contains alphanumeric data. How do I do this?
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 15 de Nov. de 2016
Here's one way:
ca = {37, 0, 240, 0.436, 'cat dog mouse', 10}
fid = open(filename, 'wt'); % File on disk
% fid = 1; % Command window.
fprintf(fid, '%f, %f, %f, %f, %s, %d\n', ca{:});
close(fid);
4 comentarios
T Shep
el 15 de Nov. de 2016
Image Analyst
el 15 de Nov. de 2016
You need %f for numbers with fractional parts. You can use %f for integers also but %d works for integers only and %d is what I use if I know for certain the numbers are pure integers. Using %d for fractional numbers won't work - you need %f for them.
T Shep
el 16 de Nov. de 2016
Image Analyst
el 16 de Nov. de 2016
I don't know. You could always try it. I think they recommend textscan() now instead of textread(). Or you could use fgetl() to get just one line and then use textscan() or sscanf() on just that line.
You might also be able to use importdata() or readtable(). See which function lets you read it in with the least fuss.
Categorías
Más información sobre Data Import and Export en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!