フィルターのクリア

about saving the numerical value and string altogether

2 ビュー (過去 30 日間)
liangjian
liangjian 2011 年 11 月 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 Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by