about saving the numerical value and string altogether

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Sven
Sven il 30 Nov 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.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings 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