Error using ==> fprintf Function is not defined for 'cell' inputs.

조회 수: 7 (최근 30일)
Himanshi Rani
Himanshi Rani 2017년 9월 20일
댓글: Guillaume 2017년 9월 20일
Hi, I am working with Tables as shown :
count EndNodes Weight
_____ ________________________ ________
'R1' {1x1 cell} {1x1 cell} 0.31486
'R2' {1x1 cell} {1x1 cell} 0.049458
'R3' {1x1 cell} {1x1 cell} 0.20583
'R4' {1x1 cell} {1x1 cell} 0.42986
'R5' {1x1 cell} {1x1 cell} 0.34631
'R6' {1x1 cell} {1x1 cell} 0.026973
I want to print the table on a notepad. But when I use the fprintf command, there is an error saying 'fprintf Function is not defined for 'cell' inputs.' My version of Matlab is 2016a. Could anyone guide me ?
  댓글 수: 2
Jan
Jan 2017년 9월 20일
Of course we must see the command before we can knwo, why it is failing.
Himanshi Rani
Himanshi Rani 2017년 9월 20일
편집: Himanshi Rani 2017년 9월 20일
nrow=size(Table,1)
fid= fopen('OutputFile.txt', 'wt');
for K = 1 : nrow
fprintf(fid, '%-20s %-20s %8f %8f\n', Table{K,1}, Table{K,2},Table{K,3});
end
fclose(fid);
This code has been given to me by Walter Roberson in one of my other questions.

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

답변 (1개)

Guillaume
Guillaume 2017년 9월 20일
As the error message says, you can't pass cell arrays to sprintf. It doesn't know what to do with them since they can contain anything (and nothing). You would have to extract the content of these cell arrays and path that content to sprintf instead.
As we don't know what is in these cell arrays, we can't tell you how to do that.
  댓글 수: 4
Himanshi Rani
Himanshi Rani 2017년 9월 20일
편집: Himanshi Rani 2017년 9월 20일
I am sorry. This is the correct cell array:
Table.EndNodes{1,2}
ans =
'N15'
It contains the endnodes of line segments.
Guillaume
Guillaume 2017년 9월 20일
You don't make it easy to help you by not giving enough information. The exact class/content of the column is required to be able to tell you how to call fprintf.
I'm going to assume that Table.EndPoint is an Nx2 cell array. Each cell containing a 1x1 cell array, and each of these cell array containg a char vector. That's the only configuration that would match your question and your latest comment. However, it does not make much sense as the 1x1 cell arrays had an unnecessary level of indirection that is not needed.
With that assumption in place:
fid = fopen('OutputFile.txt', 'wt');
rowfun(@(count, endnodes, weight) fprintf(fid, '%s %s %s %f\n', count, endnodes{:}, weight), Table, 'NumOutputs', 0, 'ExtractCellContent', true);
fclose(fid);
You will have to adjust the format string according to what you want.

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

카테고리

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