How to code fprintf on multiple lines ?
13 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm trying to write an fprintf statement on multiple lines of code so that i don't exceed a 85 columns boundary (half my screen).
I can't find a solution that don't need to use another function (like strcat) :
fprintf(strcat("Les données des deux sessions de '%s' semblent venir de",...
" deux distributions différentes.\n p = %4.3f \n"), results_name{i}, pp);
When I try
fprintf(["Les données des deux sessions de '%s' semblent venir de"...
" deux distributions différentes.\n p = %4.3f \n"], results_name{i}, pp);
I get an error :
% Error using fprintf
% Invalid file identifier. Use fopen to generate a valid file identifier.
Do I really need to use a function to do so, or is there another way ?
Thank you !
0 个评论
采纳的回答
Rik
2019-9-18
As far as I'm aware this should work with strings as well, but I frequently use a setup like this with a char array instead. So if you replace double quotes by single quotes this should work with a square bracket without requiring a call to strcat.
3 个评论
Rik
2019-9-18
编辑:Rik
2019-9-18
The problem with your original code is that it generates a 1x2 string array as the FormatSpec. I can't find in the doc how you can expect fprintf to handle a non-char non-scalar FormatSpec, but I think we have the conclusion: poorly. It might attempt to treat the first string element as the fid and the second as FormatSpec, but I'm not aware of a robust way to test this hypothesis.
Also, the square bracket might be calling strcat (instead of cat) for char inputs. The debugger on my copy of R2019a doesn't allow me to step into this operation.
更多回答(1 个)
Ruger28
2019-9-18
why are you using
strcat
instead of
sprintf
Also, your data would be helpful, but try:
fprintf(sprintf("Les données des deux sessions de '%s' semblent venir de",...
" deux distributions différentes.\n p = %4.3f \n", results_name{i}, pp));
4 个评论
Ruger28
2019-9-18
I don't believe there is a way to do that on non-comments, so the scroll bar is most likely your only option.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!