Error using fprintf for cells

I am using following code to generate text file from array which has 1 matrix of 496 * 6 size with strings in it. I m getting 'Bad cell reference operation.' error
Code:
fileID = fopen('Gcode.txt','w');
fprintf(fileID,'%10s %10s %10s %10s %10s %10s \n', G_code{1,1}{:}, G_code{1,1} {:},G_code{1,1}{:},G_code{1,1}{:},G_code{1,1}{:},G_code{1,1}{:});
fclose(fileID);
Can anyone explain?

 채택된 답변

Cedric
Cedric 2013년 7월 25일
편집: Cedric 2013년 7월 25일

2 개 추천

G_code{1,1}{:} is a comma separated list (CSL), which is not - I guess - what you thought it was. It is not possible to tell you what is wrong without knowing what you store in G_code and how you want it written in the file, but to illustrate, if G_code{1,1} where the cell array {'hello', 'world'}, the following line
fprintf('%s %s\n', G_code{1,1}{:}) ;
would be equivalent to
fprintf('%s %s\n', G_code{1,1}{1}, G_code{1,1}{2}) ;
or taking the content
fprintf('%s %s\n', 'hello', 'world') ;
So you see that {:} returns a list of cells' content separated by commas.
EDIT: as FPRINTF repeats its formatSpec until all its args are displayed, you can use CSL the following way: assume you want to have two columns in the output with the data
>> data = {'A1', 'A2'; 'B1', 'B2'} ;
you can specify a formatSpec with two column, and develop data as a CSL (but just once)
>> fprintf('%s %s\n', data{:}) ;
A1 B1
A2 B2
which is equivalent to
>> data_col = data(:) ;
>> fprintf('%s %s\n', data_col{1}, data_col{2}, data_col{3}, data_col{4}) ;

댓글 수: 4

siddhesh rane
siddhesh rane 2013년 7월 25일
my cell array is as shown below..
'N1' 'G0' 'X0 'Y0' 'Z0' [] < -- row 1
'N1' 'G20' 'X30 'Y40' 'Z50' [] <--- row 2
Cedric
Cedric 2013년 7월 25일
편집: Cedric 2013년 7월 25일
So you will want to transpose your data first, because reading it with (:) or {:} will read column first. Illustration:
>> G_code{1,1} = {'N1' 'G0' 'X0' 'Y0' 'Z0' []; ...
'N1' 'G20' 'X30' 'Y40' 'Z50' []} ;
>> buffer = G_code{1,1}.' ;
>> fprintf('%10s %10s %10s %10s %10s %10s \n', buffer{:}) ;
N1 G0 X0 Y0 Z0
N1 G20 X30 Y40 Z50
siddhesh rane
siddhesh rane 2013년 7월 25일
its showing error:
Cell contents reference from a non-cell array object.
Error in readSTLexp13 (line 599)
fprintf('%10s %10s %10s %10s %10s %10s \n', buffer{:}) ;
Cedric
Cedric 2013년 7월 25일
How did you define buffer? based on your initial code, I was assuming that G_code is a cell array, and that cell (1,1)'s content is again a cell array with the strings. However, I suspect that you made a mistake in the way you are addressing G_code's content .. how is it defined?

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

태그

질문:

2013년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by