필터 지우기
필터 지우기

How to print \ in fprintf?

조회 수: 3 (최근 30일)
Jay Vaidya
Jay Vaidya 2020년 7월 21일
답변: Cris LaPierre 2020년 7월 21일
I have to print a command to a file,
lib_def = '.lib C:\Users\vaidy\Documents\LTspiceXVII\lib\cmp\standard.mos\n'
Opamp_lib = '.lib UniversalOpamps2.sub\n'
File = fopen('oscillator_8.cir','w');
fprintf(File,sprintf(lib_def));
fprintf(File,Opamp_lib);
fclose(File);
I'm using the above commands, but it doesn't print the same to the file.
Output:
.lib C:.lib UniversalOpamps2.sub

채택된 답변

Walter Roberson
Walter Roberson 2020년 7월 21일
lib_def = '.lib C:\Users\vaidy\Documents\LTspiceXVII\lib\cmp\standard.mos' %no \n here!
fprintf(File,'%s\n', lib_def);

추가 답변 (2개)

Star Strider
Star Strider 2020년 7월 21일
You need to ‘escape’ the backslant (\) with a preceding backslant (\\) in order to print it as you want to.
Perhaps this will do what you want:
lib_def = '.lib C:\\Users\\vaidy\\Documents\\LTspiceXVII\\lib\\cmp\\standard.mos\n'
I am assuming here that you want to retain the ‘\n’ as a newline character.
.

Cris LaPierre
Cris LaPierre 2020년 7월 21일
You have not specified the formatspec input argument. Even with that, special characters like a backslash need to be handled differently. See the Text Before or After Formatting Operators section in the linked documentation page. Here, that means use "\\" when you wnat "\". Below is an example that prints to the screen, allowing you to verify the behavior.
lib_def = '.lib C:\\Users\\vaidy\\Documents\\LTspiceXVII\\lib\\cmp\\standard.mos\n'
fprintf(sprintf('%s',lib_def));

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by