MATLAB Answers

frprintf into a file with adding the symbol for Single quote

1 view (last 30 days)
Maximilian Bayerl
Maximilian Bayerl on 29 Apr 2021
Commented: Rik on 29 Apr 2021
Hello Everyone,
I have a textfile, which I created with matlab. Later, I want to use the text-file with a schme-macro in a simulation programm. I need every line in the textfile like this:
(define bla '(0 0 0 1 1 1)). The zeros and ones represent different possibilites.
My poroblem so far is, that I can't frpint the symbol ', due to the fact, that this symbol marks for matlab the end of the text in my code.
I am that far:
fid1 = fopen('test1.txt');
fid2 = fopen('test2.txt','w');
tline = fgets(fid1);
Character2add = '(define pyrx ('; % here put the character to be added
while ischar(tline)
disp(tline)
fprintf(fid2,'%s %s\n',Character2add,tline);
tline = fgets(fid1);
end
fclose(test1);
fclose(test);
What I got with this is:
'(define bla ('0 0 0 1 1 1
What I need now is:
  • I need the last 2 paranthesis at the end of each line
  • I need the single-quote symbol ' after my bla

Accepted Answer

Steven Lord
Steven Lord on 29 Apr 2021
fprintf('There''s two main ways to print a single quote')
There's two main ways to print a single quote
fprintf('Two single quotes '''' in a char format specifier prints a single quote '' in the result')
Two single quotes '' in a char format specifier prints a single quote ' in the result
fprintf("Or if you're using a release that supports string, specify the format as a string")
Or if you're using a release that supports string, specify the format as a string
  3 Comments
Rik
Rik on 29 Apr 2021
Seventh:
fprintf('There are several apostrophes in Unicode:\n39: %c\n700: %c\n1370: %c\nand probably more',39,700,1370)
There are several apostrophes in Unicode: 39: ' 700: ʼ 1370: ՚ and probably more

Sign in to comment.

More Answers (1)

VBBV
VBBV on 29 Apr 2021
Edited: VBBV on 29 Apr 2021
%if
%if
Character2add = ['','(define pyrx','''(']
Use this
  2 Comments
Stephen Cobeldick
Stephen Cobeldick on 29 Apr 2021
Note:
['','(define pyrx','''(']
^^ provides zero characters to the final character vector (i.e. does nothing)
Why concatenate three character vectors? Why not just define one character vector directly? Simpler:
'(define pyrx''('

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by