please help me to rewrite this function code correctly

Serobinfo=fopen('asri.m','w');
fprintf(Serobinfo,'function [B,C]=as(nudat,tdr)\n\nD=nudat;\nE=tdr;\nif D==1\nif E==1\nB={'q1' 'pi/2' '0' '7.53';'q2' '0' '16.83' '0';'q3' '0' '11.43' '0';'q4' 'pi/2' '0' '8.20';'q5' '0' '0' '9.20';'-g1' '0' '0' '0';'0' '0' 'g2' '0';'g3' '0' '0' '0';'0' '0' 'g4' '0'};\nC=9;\nelse\nerrordlg('No tenemos ese robot disponible','Elija otro opción por favor');\nend\nelse\nerrordlg('Ninguno de los robots cumple para esos grados de libertad','Elija otro opción por favor');\nend');
fclose(Serobinfo);
The problem is because the following error arises in matlab as it is written Error: Unexpected MATLAB expression.
(if that it's imposible a short explanation could be helpful) Thanks in advance

 Risposta accettata

Serobinfo=fopen('asri.m','wt');
fprintf(Serobinfo,'function [B,C]=as(nudat,tdr)\n\nD=nudat;\nE=tdr;\nif D==1\nif E==1\nB={''q1'' ''pi/2'' ''0'' ''7.53'';''q2'' ''0'' ''16.83'' ''0'';''q3'' ''0'' ''11.43'' ''0'';''q4'' ''pi/2'' ''0'' ''8.20'';''q5'' ''0'' ''0'' ''9.20'';''-g1'' ''0'' ''0'' ''0'';''0'' ''0'' ''g2'' ''0'';''g3'' ''0'' ''0'' ''0'';''0'' ''0'' ''g4'' ''0''};\nC=9;\nelse\nerrordlg(''No tenemos ese robot disponible'',''Elija otro opción por favor'');\nend\nelse\nerrordlg(''Ninguno de los robots cumple para esos grados de libertad'',''Elija otro opción por favor'');\nend');
fclose(Serobinfo);
You forgot that ' ends the string and that you need '' to indicate a single '

2 Commenti

Serobinfo=fopen('asri.m','w');
fprintf(Serobinfo,'function [B,C]=as(nudat,tdr)\n\nD=nudat;\nE=tdr;\nif D==1\nif E==1\nB={'q1' 'pi/2' '0' '7.53';'q2' '0' '16.83' '0';'q3' '0' '11.43' '0';'q4' 'pi/2' '0' '8.20';'q5' '0' '0' '9.20';'-g1' '0' '0' '0';'0' '0' 'g2' '0';'g3' '0' '0' '0';'0' '0' 'g4' '0'};\nC=9;\nelse\nerrordlg('No tenemos ese robot disponible','Elija otro opción por favor');\nend\nelse\nerrordlg('Ninguno de los robots cumple para esos grados de libertad','Elija otro opción por favor');\nend');
fclose(Serobinfo);
No. Remember that a single apostrophe, ' ends the string. So it starts looking through
fprintf(Serobinfo,'function [B,C]=as(nudat,tdr)\n\nD=nudat;\nE=tdr;\nif D==1\nif E==1\nB={'q1' 'pi/2' [....]
and it sees the ' before function to start the string. It continues along and sees the single ' between { and q1 and that signals the end of the string. Then it tries to figure out what a quoted string followed by q1 means but there is no permitted meaning for it.
The portion that I extracted there would have to be
fprintf(Serobinfo,'function [B,C]=as(nudat,tdr)\n\nD=nudat;\nE=tdr;\nif D==1\nif E==1\nB={''q1'' ''pi/2'' [....]
The ' before function starts the string. It continues along and sees the ' between { and 'q1 but it knows that two ' in a row, '' means that you want to put in a literal ' at that point in the string, so it knows that you are still in the string and continues along without problem.
If it makes it easier for you, use
S = 'function [B,C]=as(nudat,tdr)\n\nD=nudat;\nE=tdr;\nif D==1\nif E==1\nB={"q1" "pi/2" "0" "7.53";"q2" "0" "16.83" "0";"q3" "0" "11.43" "0";"q4" "pi/2" "0" "8.20";"q5" "0" "0" "9.20";"-g1" "0" "0" "0";"0" "0" "g2" "0";"g3" "0" "0" "0";"0" "0" "g4" "0"};\nC=9;\nelse\nerrordlg("No tenemos ese robot disponible","Elija otro opción por favor");\nend\nelse\nerrordlg("Ninguno de los robots cumple para esos grados de libertad","Elija otro opción por favor");\nend');
S(S == '"') = '''';
fprintf(Serobinfo, S);
Or even
S(S == '"') = char(39); %single apostrophe

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by