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

 채택된 답변

Walter Roberson
Walter Roberson 2017년 2월 17일
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

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

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by