How to resolve this error Error using fprintf Function is not defined for 'cell' inputs.

9 vues (au cours des 30 derniers jours)
i want to find out k-shortest path between source and distination. i have creat a matrix for source and distination and for k-shortrst path i have creat a cell arrya in which each cell have three fixed number of path that is shown by hope count (e.g., source distination [hope counts for path1, hope counts for path 2, hope counts for path 3rd]) (e.g., 1 2 [3 4 5]).
n=6;
C1i={[0],[1 2 4],[2 3 4],[1 2 4],[2 3 4],[3 4 5]};
C2i={[1 2 4],[0],[1 3 4],[1 2 3],[2 3 3],[2 3 4]};
C3i={[2 3 4],[1 3 4],[0],[2 3 3],[1 2 3],[1 2 4]};
C4i={[1 2 4],[1 2 3],[2 3 3],[0],[1 3 4],[2 3 4]};
C5i={[2 3 4],[2 3 3],[1 2 3],[1 3 4],[0],[1 2 4]};
C6i={[3 4 5],[2 3 4],[1 2 4],[2 3 4],[1 2 4],[0]};
h = [C1i; C2i; C3i; C4i; C5i; C6i];
for i = 1:n
for j = 1:n
if i ==n && j == n
fprintf('%d %d %s ;\n', i, j, h{i, j});
else
fprintf('%d %d %s\n', i, j, h{i, j});
end
end
which give me output like this..
1 1

Réponses (2)

Bruno Luong
Bruno Luong le 7 Sep 2019
Modifié(e) : Bruno Luong le 7 Sep 2019
Not sure the formatting display you want, I simply fix some of issues your code so it can run
n=6;
C1i={[0],[1 2 4],[2 3 4],[1 2 4],[2 3 4],[3 4 5]};
C2i={[1 2 4],[0],[1 3 4],[1 2 3],[2 3 3],[2 3 4]};
C3i={[2 3 4],[1 3 4],[0],[2 3 3],[1 2 3],[1 2 4]};
C4i={[1 2 4],[1 2 3],[2 3 3],[0],[1 3 4],[2 3 4]};
C5i={[2 3 4],[2 3 3],[1 2 3],[1 3 4],[0],[1 2 4]};
C6i={[3 4 5],[2 3 4],[1 2 4],[2 3 4],[1 2 4],[0]};
h = [C1i; C2i; C3i; C4i; C5i; C6i];
for i = 1:n
for j = 1:n
if i ==n && j == n
fprintf('%d %d %s ;\n', i, j, mat2str(h{i, j}));
else
fprintf('%d %d %s\n', i, j, mat2str(h{i, j}));
end
end
end
  2 commentaires
Irfanullah Khan
Irfanullah Khan le 8 Sep 2019
i have modified my code according to your comments but this can't work with my code. It show the following errors. Kindly have a look to guide me further
GLPSOL: GLPK LP/MIP Solver, v4.60
Parameter(s) specified in the command line:
--model Encryption.mod --data Encryption.dat --output Encryption.sol
Reading model section from Encryption.mod...
Encryption.mod:168: warning: unexpected end of file; missing end statement inserted
168 lines were read
Reading data section from Encryption.dat...
Encryption.dat:72: one item missing in data group beginning with param
Context: ...6 1 1 6 2 1 6 3 1 6 4 1 6 5 1 6 6 0 ; param h := param B1 :=
MathProg model processing error
Bruno Luong
Bruno Luong le 8 Sep 2019
You show the error somewhere else, nothing apparently related to the FPRINTF original question.
How do you expect us to solve it for you?

Connectez-vous pour commenter.


Guillaume
Guillaume le 7 Sep 2019
Modifié(e) : Guillaume le 7 Sep 2019
This is how I'd do it:
hstr = cellfun(@(v) strjoin(compose("%d", v), " "), h); %convert each vector in h into a string
[row, col] = ndgrid(1:size(hstr, 1), 1:size(hstr, 2)); %get all indices of rows and columns
lines = compose('%d %d: %s', row(:), col(:), hstr(:)); %use any formatting you want. I've added a : for clarity
fprintf([strjoin(lines, '\n'), ';\n'])
Note that I'm mixing strings and cell arrays of char vectors on purpose to simplify the code.
  2 commentaires
Irfanullah Khan
Irfanullah Khan le 7 Sep 2019
Bundle of thanks for your time, consideration and prompt comments. Actually i am using glpk for optimization of the problem (i.e minimize the cost). For the data transmission single path (i.e., shortest) i have creat a matrix h=[0 1 2 1 2 3;1 0 1 1 2 2; 2 1 0 2 1 1;1 1 2 0 1 2;2 2 1 1 0 1;3 2 1 2 1 0];
Now i want to extend it for K- paths not for a single. the above mention code can use for it? or is there is any solution in your mind can be possable, kindly share it with me. i shell be very thankful for your this act of kindness.
Best regards,
Khan
Guillaume
Guillaume le 8 Sep 2019
I have absolutely no idea what you're asking but it doesn't appear to be related to your original question. The only thing the above does is print your cell array to the command window. It will work with cell arrays of any size as long as the content of the cells is just vectors (of any length).

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by