about saving the numerical value and string altogether

2 visualizaciones (últimos 30 días)
liangjian
liangjian el 30 de Nov. de 2011
I have a string matrix with N rows and 2 columns. Each cell stores a string. I have another N*1 vector, where each entry is a numerical value.
How can I save these two structures into a single text file row by row.
In other words, each row of the saved text file is composed of three elements, the first two elements come from a row of the string matrix and the third element comes from the corresponding row of that vector. Thanks.

Respuesta aceptada

Sven
Sven el 30 de Nov. de 2011
Hi Liangjian, try this to join your cell and numbers:
myCell = {'asf','qwe';'wer','two';'opi','oei'};
myNums = rand(3,1);
allData = [myCell num2cell(myNums)]'; % Note the "'", used for saving below
Now, to save the result to a comma-separated text file:
fid = fopen('myFile.txt','w');
fprintf(fid,'%s,%s,%f\n', allData{:})
fclose(fid)
Note that saving to a text file is a good idea if you need to access your data outside of MATLAB. If you only need to load the data into MATLAB again later, there are native MATLAB save() and load() commands that are probably better than converting to text files.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by