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 !

 채택된 답변

Rik
Rik 2019년 9월 18일

1 개 추천

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

Oh thanks it worked ! I don't quite understand why but nvm :)
fprintf(['Les données des deux sessions de ''%s'' semblent venir',...
'd''une même distribution.\n p = %4.3f \n'], results_name{i}, pp);
Rik
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.
Carrillo Pablo
Carrillo Pablo 2019년 9월 18일
Thanks a lot for the explanation. I wasn't aware there was a difference between single and double quotes, neither than they were creating a different class of array, I guess I do now !

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ruger28
Ruger28 2019년 9월 18일

0 개 추천

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

Carrillo Pablo
Carrillo Pablo 2019년 9월 18일
편집: Carrillo Pablo 2019년 9월 18일
Yeah sorry for the lack of context but thanks for the help, let's make it more simple :
a=1;
b=2;
fprintf(strcat("Lorem ipsum dolor sit amet, %i consectetur adipiscing",...
"quis nostrud exercitation ullamco laboris %i\n"), a, b);
I want to display the string on the command window and that the code don't exceed my limit of 85 width. Using strcat seems a bit overkill just to have a linebreak...
I don't quite understand the specificities of fprintf but can't achieve that using
disp()
Ruger28
Ruger28 2019년 9월 18일
what do you wish to be size limited: the output string or your code in the script editor?
Carrillo Pablo
Carrillo Pablo 2019년 9월 18일
The code in the editor.
Ruger28
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

태그

질문:

2019년 9월 18일

댓글:

2019년 9월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by