about saving the numerical value and string altogether

조회 수: 1 (최근 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 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