How can I get the sprintf function to work for a cell array and a matrix?
이전 댓글 표시
I am writing a code in which I need to use the sprintf function to display the output values. The output values include 2 cell arrays and 5 matrices. For whatever reason I keep getting this error:
Error using sprintf
Function is not defined for 'cell' inputs.
This is the code I am currently using:
output(2)= {sprintf('%s \t %s \t %.3f \t %.3f \t %.3f \t %.3f \t %.3f \t %.3f', probe_label, gene_label, [dtAm], [dtAstd]', [dtBm], [dtBstd]', [dtAB], [dt_pval_sig])};
probe_label and gene_label are the cell arrays and the rest are matrices.
답변 (1개)
Star Strider
2015년 2월 6일
You have to convert them with char to use sprintf and its friends:
probe_label = {'abc' 'def' 'ghi' 'jkl'};
gene_label = {'mno' 'pqr' 'stu' 'vwx'};
[dtAm, dtAstd, dtBm, dtBstd, dtAB, dt_pval_sig] = deal(rand(4,1), rand(1,4), rand(4,1), rand(1,4), rand(1,4), rand(1,4));
for k1 = 1:size(probe_label,2)
output{k1,:} = sprintf('%s \t %s \t %.3f \t %.3f \t %.3f \t %.3f \t %.3f \t %.3f', char(probe_label(k1)), char(gene_label(k1)), dtAm(k1), dtAstd(k1), dtBm(k1), dtBstd(k1), dtAB(k1), dt_pval_sig(k1));
end
I created the data, but this code should work with your data without modification (I hope).
댓글 수: 7
S
2015년 2월 6일
Star Strider
2015년 2월 6일
I copied your sprintf call exactly. I had to make some changes to it to make it work, but I did not change the argument list. If you run my code independently, it works.
Most likely, you have already defined a variable named ‘output’ earlier in your code.
To check for that possibility, before the loop, put the statement:
whos output
It will tell you if ‘output’ exists, and what size it is. If you have already declared it, then rename it or the for loop variable something else.
S
2015년 2월 7일
Star Strider
2015년 2월 7일
I’m running R2014b. There could be version differences, but I tested it. (I test all my code if possible, and if not, I specifically label it as untested code.) It ran for me without error.
When I run it, it produces:
output =
'abc mno 0.001 0.127 0.780 0.050 0.841 0.220'
'def pqr 0.030 0.009 0.437 0.091 0.857 0.226'
'ghi stu 0.208 0.727 0.437 0.594 0.964 0.537'
'jkl vwx 0.455 0.354 0.049 0.241 0.489 0.762'
I have no idea what could be wrong with your system that it would not run on it.
Jan
2015년 2월 7일
char(gene_label(k1)) is remarkably less efficient than gene_label{k1}.
S
2015년 2월 9일
Star Strider
2015년 2월 9일
Your numeric data aren’t cells. Change the ‘output’ assignment to:
output{k1,:} = sprintf('%s \t %s \t %.3f \t %.3f \t %.3f \t %.3f \t %.3f \t %.3f', probe_label{k1}, gene_label{k1}, dtAm(k1), dtAstd(k1), dtBm(k1), dtBstd(k1), dtAB(k1), dt_pval_sig(k1));
and it should work
I didn’t test this change specifically, but it is only a minor variation on my original code. Note that only ‘probe_label{k1}’ and ‘gene_label{k1}’ have the curly-braces ‘{}’ cell reference, while your numeric arrays have standard parentheses ‘()’ array references. The referencing conventions can be a bit difficult to understand until you work with them.
카테고리
도움말 센터 및 File Exchange에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!