about saving the numerical value and string altogether

1 次查看(过去 30 天)
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.

采纳的回答

Sven
Sven 2011-11-30
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by