function []=printresults2file(shape)
fid=fopen('ChangyuLiu.txt','wt');
fprintf(fid, 'The number of entered objects was %d \n', size(shape,1));
fprintf(fid,'%s \t\t %s \t\t %s \t\t %s\n','No.','ID','Color','Area');
for i=1:size(shape,1)
fprintf(fid, '%d %s %s %f',shape{i,:});
fprintf('\n');
end
fclose(fid);
end
This is a sub-function I used to output a table in .txt file. However, the error kept pumping up as
Error using fprintf
Function is not defined for 'cell' inputs.
I wonder what's causing the problem.

6 件のコメント

James Tursa
James Tursa 2020 年 4 月 2 日
編集済み: James Tursa 2020 年 4 月 2 日
What is in shape, specifically what does shape{i,:} produce?
Also, is fprintf('\n') supposed to be fprintf(fid,'\n')?
Walter Roberson
Walter Roberson 2020 年 4 月 2 日
Some of your shape{i,2} or shape{i,3} are probably cell array of character vectors instead of being directly character vectors. Or possibly shape{i,1} or shape{i,4} is a cell containing a number.
Changyu Liu
Changyu Liu 2020 年 4 月 2 日
The shape is a cell array that is created by a while loop, which contains four columns and unknown number of rows depending on the user input.
Walter Roberson
Walter Roberson 2020 年 4 月 2 日
Please show us
disp(shape(1,:))
Changyu Liu
Changyu Liu 2020 年 4 月 2 日
>> disp(shape(1,:))
[1] {1×1 cell} {1×1 cell} [3.1416]
James Tursa
James Tursa 2020 年 4 月 3 日
So that is your answer, two of the cell elements are themselves cells, and you can't feed those directly to fprintf.

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2020 年 4 月 3 日

0 投票

What happens if you change this
fprintf(fid, '%d %s %s %f',shape{i,:});
to this
fprintf(fid, '%d %s %s %f',shape{i,1},shape{i,2}{1},shape{i,3}{1},shape{i,4});

1 件のコメント

Changyu Liu
Changyu Liu 2020 年 4 月 3 日
Much appreciated!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File 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