Write a matrix of strings to text file:

조회 수: 50 (최근 30일)
Ionut  Anghel
Ionut Anghel 2015년 6월 25일
댓글: Stephen23 2015년 6월 25일
Hi, I have the following problem. I have to write a kind of header to text file. I tried to create a matrix (rather I should say an array) but when I tried to write I get error "Error using fprintf Function is not defined for 'cell' inputs." Here is the code:
{
vect_signals={'T' '211A015' '211B035' '211A101' '211B111' '211N401' '211D411' '21411A151' '21411K152' '21411Q501_2'...
'21411Q503_2' '21411Q505_2' '21411Q507_2' '21411Q509_2' '21411Q511_2' '22411Q151' '22411Q152' '22411Q501_2'...
'22411Q503_2' '22411Q505_2' '22411Q507_2' '22411Q509_2' '22411Q511}
vect_zeros=zeros(length(vect_signals),1);
vect_Val_zeros=num2str(vect_zeros);
matrix_signals={vect_signals01; vect_Val_zeros;};
fid01=fopen('myheader.txt','w' );
for i=1:length(vect_signals01)
fprintf(fid01,'%14s %10s \n',matrix_signals{i,:});
end
fclose('all');
}
I think it suppose to work even without loop.
The final result in the text file should be:
'T' 0
'211A015' 0
'211B035' 0
'211A101' 0
........................
  댓글 수: 2
Stephen23
Stephen23 2015년 6월 25일
There are several syntax errors here, such as the
{
code...
}
parentheses, and also a missing quotation mark on the last string of vect_signals. This code does not even run and it generates multiple error messages in the editor.
dpb
dpb 2015년 6월 25일
Do you really want the quote in the file?

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

채택된 답변

Stephen23
Stephen23 2015년 6월 25일
편집: Stephen23 2015년 6월 25일
Try this:
vect_signals = {'T', '211A015', '211B035', '211A101', '211B111', '211N401', '211D411',...
'21411A151', '21411K152', '21411Q501_2', '21411Q503_2', '21411Q505_2', '21411Q507_2',...
'21411Q509_2', '21411Q511_2', '22411Q151', '22411Q152', '22411Q501_2', '22411Q503_2',...
'22411Q505_2', '22411Q507_2', '22411Q509_2', '22411Q511'};
vect_out(2,:) = num2cell(zeros(size(vect_signals)));
vect_out(1,:) = vect_signals;
fid = fopen('temp.txt','wt');
fprintf(fid,'%-14s %d\n',vect_out{:});
fclose(fid);
Which generates this file:
T 0
211A015 0
211B035 0
211A101 0
211B111 0
211N401 0
211D411 0
21411A151 0
21411K152 0
21411Q501_2 0
21411Q503_2 0
21411Q505_2 0
21411Q507_2 0
21411Q509_2 0
21411Q511_2 0
... ETC
  댓글 수: 2
Ionut  Anghel
Ionut Anghel 2015년 6월 25일
It works very nice. Thank you.
Stephen23
Stephen23 2015년 6월 25일
And if you need the quotation marks around the strings, then simply change this line
vect_out(1,:) = vect_signals;
to this:
vect_out(1,:) = strcat('''',vect_signals,'''');

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

추가 답변 (1개)

dpb
dpb 2015년 6월 25일
" I get error "Error using fprintf Function is not defined for 'cell' inputs."
Yes. Cast the cell content to char() when writing...
You also need to build the vect_signals array with a delimiter between the elements; otherwise Matlab simply concatenates them into a long single string.
vect_signals={'T'; '211A015'; ...
  댓글 수: 1
Stephen23
Stephen23 2015년 6월 25일
편집: Stephen23 2015년 6월 25일
This is only true for the concatenation operators [], not the cell array constructors {}. But it is certainly preferred to use explicit separators.
>> X = {'T' '211A015' '211B035'}
X =
'T' '211A015' '211B035'
>> size(X)
ans =
1 3

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by