fprintf: Function is not defined for 'cell' inputs.

18 views (last 30 days)
Can someone say what is wrong with my code?
the errors are: *??? Error using ==> fprintf Function is not defined for 'cell' inputs.
Error in ==> TESTAR at 37 fprintf('Problem statement:\n%s', problem_statement);*
my code is:
clear; run('..\psg_load_libraries'); addpath('..');
clear problem_statement;
problem_statement(1)= textread('problem_DEA_Hospital.txt','%s', 'whitespace', ''); problem_statement(2)= textread('problem_DEA_Hospital2.txt','%s', 'whitespace', '');
% read and print problem statement
for i=1:1:2
problem_statement=problem_statement(i);
fprintf('Problem statement:\n%s', problem_statement);
end

Accepted Answer

Kalyne
Kalyne on 17 Jun 2013
Hey Evan, Thanks for you answer.. I tryed to change it but it keeps given me the same message. the new code is like that:
clear problem_statement;
problem{1}= textread('problem_DEA_Hospital.txt','%s', 'whitespace', ''); problem{2}= textread('problem_DEA_Hospital2.txt','%s', 'whitespace', '');
% read and print problem statement
for i=1:1:2
problem_statement=problem{i};
fprintf('Problem statement:\n%s', problem_statement);
  2 Comments
Evan
Evan on 17 Jun 2013
Edited: Evan on 17 Jun 2013
Whenever you call textread, try changing problem{1} back to problem(1). Same thing for problem{2}.
Because () accesses the cell and {} accesses the contents of a cell, if textread returns a cell and you're putting it in the contents of another cell, that means you're getting a cell inside a cell. So even though you remove the contents of the cell for your variable that you use in fprintf, there's still one more "layer" of cell there that's tripping up the code.
Evan
Evan on 17 Jun 2013
Edited: Evan on 17 Jun 2013
Actually, I just thought about it, and either way a cell array will be returned. You might have to convert problem_statement to a character array when using fprintf even after accessing the contents of problem.
clear problem_statement;
problem{1} = textread('problem_DEA_Hospital.txt','%s', 'whitespace', ''); problem{2} = textread('problem_DEA_Hospital2.txt','%s', 'whitespace', '');
% read and print problem statement
for i = 1:2
problem_statement = char(problem{i});
fprintf('Problem statement:\n%s', problem_statement);
Sorry about the confusion. I don't use textscan very much didn't consider the way it returned data. Hopefully that solves it.

Sign in to comment.

More Answers (1)

Evan
Evan on 17 Jun 2013
Edited: Evan on 17 Jun 2013
It sounds like "problem_statement" is a cell array, and fprintf cannot accept cells as arguments. What if you change the second to last line to this:
problem_statement=problem_statement{i};
That will cause your new definition of "problem_statement" to access the contents of the ith cell of the problem_statement cell array instead of the ith cell itself.
Note, though, that I don't think the second iteration of your code will run correctly if you're redefining problem_statement as a single-element array before trying to later access the second element. You might want to give problem_statement a different name in the loop instead of redefining it.

Products

Community Treasure Hunt

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

Start Hunting!