Really! fprintf cell error

1 vue (au cours des 30 derniers jours)
Changyu Liu
Changyu Liu le 2 Avr 2020
Commenté : Changyu Liu le 3 Avr 2020
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 commentaires
Changyu Liu
Changyu Liu le 2 Avr 2020
>> disp(shape(1,:))
[1] {1×1 cell} {1×1 cell} [3.1416]
James Tursa
James Tursa le 3 Avr 2020
So that is your answer, two of the cell elements are themselves cells, and you can't feed those directly to fprintf.

Connectez-vous pour commenter.

Réponse acceptée

James Tursa
James Tursa le 3 Avr 2020
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 commentaire
Changyu Liu
Changyu Liu le 3 Avr 2020
Much appreciated!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by