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!