필터 지우기
필터 지우기

adding a label to a list of values

조회 수: 4 (최근 30일)
Jenny
Jenny 2011년 9월 25일
I have an m-file that will output a row of values to the same output file repeatedly, thus adding a new row each time I run this m-file. I need to label each new row with a unique identifier so later I remember which row corresponds to what set of data. Ideally I would like each row to look like this: Idenifier value1 value2 value3. However, I don't know where in my code I should add the identifier info to get the end result. Any ideas?
Here is the code I have so far, it's tested and it works.
p = [p1 p2 p3];
load('PlottingMatrix.txt');
plottingmatrix = PlottingMatrix;
plottingmatrix = [plottingmatrix
p];
dlmwrite('PlottingMatrix.txt', plottingmatrix)

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 25일
dlmwrite() can only write matrix. What you need is a cell array that can combine string and numerical data, such as
a={'xdc1658',1,2,3}
To write that type data, fprintf() is more suitable. Check fopen(), fclose() too for examples.
Another hint is that you don't have to read and write previous data again and again, you can use fopen('PlottingMatrix.txt','wta') to specify that you want to append the text file. You only need to write the new data and it will be appended to the end of file.
  댓글 수: 1
Jan
Jan 2011년 9월 25일
+1: Agreed. FPRINTF is not only better than LOAD and DLMWRITE, it even works.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jenny
Jenny 2011년 9월 25일
p.s. the identifier will be something like this 'xdc1658', which is why I am having a hard time adding it to the p vector in the first line.

Jenny
Jenny 2011년 9월 25일
Thanks for the tip, however, I am still stuck. This is what I have now:
p = {'Swo61670',p1,p2,p3};
fid = fopen('PlottingMatrix.txt', 'a')
count = fwrite(fid, p);
count = fprintf(fid, '%c', PlottingMatrix)
When I try to use fwrite to add the vector p to the file, I get an error message: Error using ==> fwrite Cannot write value: unsupported class cell. But I can't figure out what else to use there. Also, I don't know what to choose for the conversion character when specifying the format string in fprintf. Thanks for your help.
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 9월 25일
count = fprintf(fid, '%s,%f,%f,%f\n', p{:});
Jenny
Jenny 2011년 9월 27일
Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by