Error in writing the output to excel file

5 views (last 30 days)
clc; clear all; close all;
syms m1 m2 s1 s2 j1 j2
I = 0;
lambda = 1060*10^-9;
M=1
M = 1
z=linspace(0.0001,10000)
z = 1×100
1.0e+04 * 0.0000 0.0101 0.0202 0.0303 0.0404 0.0505 0.0606 0.0707 0.0808 0.0909 0.1010 0.1111 0.1212 0.1313 0.1414 0.1515 0.1616 0.1717 0.1818 0.1919 0.2020 0.2121 0.2222 0.2323 0.2424 0.2525 0.2626 0.2727 0.2828 0.2929
wo = 0.02;
C = 10^(-7);
k=2*pi/lambda;
po=(0.545*C^2*k^2*z).^(-(3/5));
b=0.1;
T1=0;
T2=0;
T3=0;
T4=0;
T5=0;
T6=0;
delta= ((1i*k)./(2*z))+(1./(wo.^2))+(1./(po.^2));
delta_star= subs(delta, 1i, -1i);
etta = delta_star - (1./(delta.*po.^4));
X=((k./(2.*z)).^2).*((1./(2.*1i.*sqrt(delta))).^M).*(1./(16.*delta.*etta));
alpha = (1i.*k./(2.*z)).*(1./(delta.*po.^2)-1);
beta_p = (b./(2.*wo)).*(1./(delta.*po.^2)+1);
beta_n = (b./(2.*wo)).*(1./(delta.*po.^2)-1);
A = (alpha.^2./etta)-((k.^2)-(4.*z.^2.*delta));
B1_p = ((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_p)./etta);
B1_n = ((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_n)./etta);
B2_p = -(((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_p)./etta));
B2_n = -(((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_n)./etta));
C1_p = ((b.^2)./(4.*wo.^2.*delta))+(((beta_p).^2)./etta);
C1_n = ((b.^2)./(4.*wo.^2.*delta))+(((beta_n).^2)./etta);
C2_p = ((b.^2)./(4.*wo.^2.*delta))+(((beta_n).^2)./etta);
C2_n = ((b.^2)./(4.*wo.^2.*delta))+(((beta_p).^2)./etta);
for m1 =0:M
T2=0;
for m2=0:M
T3=0;
for s1=0:m1/2
T4=0;
for j1=0:m1-2*s1
T5=0;
for s2=0:(M-m1)/2
T6=0;
for j2=0:(M-m1-(2*s2))
e1=(exp(C1_n).*hermiteH(m2+j2,((1i.*beta_n)./sqrt(etta))))+(exp(C2_p).*hermiteH(m2+j2,((-1i.*beta_p)./sqrt(etta))));
e2=(exp(C1_p).*hermiteH(m2+j2,((1i.*beta_p)./sqrt(etta))))+(exp(C2_n).*hermiteH(m2+j2,((-1i.*beta_n)./sqrt(etta))));
e3=(exp(C1_p).*hermiteH(M-m2+j2,((1i.*beta_p)./sqrt(etta))))+(exp(C2_n).*hermiteH(M-m2+j2,((-1i.*beta_n)./sqrt(etta))));
e4=(exp(C1_n).*hermiteH(M-m2+j2,((1i.*beta_n)./sqrt(etta))))+(exp(C2_p).*hermiteH(M-m2+j2,((-1i.*beta_p)./sqrt(etta))));
T6=T6+nchoosek(M-m1-(2.*s2),j2).*((1./(2.*1i.*sqrt(etta))).^(M-m2+j2)).*((1./(po.^2)).^j2).*((b./2.*wo).^(M-m1-2*s2-j2)).*e1.*e2.*e3.*e4;
end
T5=T5+T6.*(((factorial(M-m1).*(-1).^s2)./(factorial(s2).*factorial(M-m1-2.*s2))).*(((2.*1i)./(delta.^(0.5))).^(M-m1-2.*s2)));
end
T4=T4+T5.*nchoosek(m1-(2.*s2),j1).*((1./(2.*1i.*sqrt(etta))).^(m2+j2)).*((1./(po.^2)).^j1)*((b./2.*wo).^(m1-2.*s1-j1));
end
T3=T3+T4.*(((factorial(m1).*(-1).^s1)./(factorial(s1).*factorial(m1-2.*s1))).*(((2.*1i)./(delta.^(0.5))).^(m1-2.*s1)));
end
T2=T2+T3*nchoosek(M,m2)*((-1i)^(M-m2));
end
T1=T1+T2*nchoosek(M,m1)*((1i)^(M-m1));
end
I0=-real(X.*T1)
I0 = 
I=vpa(I0)
I = 
writematrix(I,"normal.xls")
Error using writematrix
Unsupported type 'sym'. Convert the data to numeric, logical, string, datetime, duration, or categorical before writing.

Accepted Answer

Karim
Karim on 7 Jul 2022
Edited: Karim on 7 Jul 2022
As indicated in the error message you need to convert the sym into a double or a string, see below for both options
syms m1 m2 s1 s2 j1 j2
I = 0;
lambda = 1060*10^-9;
M=1;
z=linspace(0.0001,10000);
wo = 0.02;
C = 10^(-7);
k=2*pi/lambda;
po=(0.545*C^2*k^2*z).^(-(3/5));
b=0.1;T1=0;T2=0;T3=0;T4=0;T5=0;T6=0;
delta= ((1i*k)./(2*z))+(1./(wo.^2))+(1./(po.^2));
delta_star= subs(delta, 1i, -1i);
etta = delta_star - (1./(delta.*po.^4));
X=((k./(2.*z)).^2).*((1./(2.*1i.*sqrt(delta))).^M).*(1./(16.*delta.*etta));
alpha = (1i.*k./(2.*z)).*(1./(delta.*po.^2)-1);
beta_p = (b./(2.*wo)).*(1./(delta.*po.^2)+1);
beta_n = (b./(2.*wo)).*(1./(delta.*po.^2)-1);
A = (alpha.^2./etta)-((k.^2)-(4.*z.^2.*delta));
B1_p = ((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_p)./etta);
B1_n = ((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_n)./etta);
B2_p = -(((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_p)./etta));
B2_n = -(((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_n)./etta));
C1_p = ((b.^2)./(4.*wo.^2.*delta))+(((beta_p).^2)./etta);
C1_n = ((b.^2)./(4.*wo.^2.*delta))+(((beta_n).^2)./etta);
C2_p = ((b.^2)./(4.*wo.^2.*delta))+(((beta_n).^2)./etta);
C2_n = ((b.^2)./(4.*wo.^2.*delta))+(((beta_p).^2)./etta);
for m1 =0:M
T2=0;
for m2=0:M
T3=0;
for s1=0:m1/2
T4=0;
for j1=0:m1-2*s1
T5=0;
for s2=0:(M-m1)/2
T6=0;
for j2=0:(M-m1-(2*s2))
e1=(exp(C1_n).*hermiteH(m2+j2,((1i.*beta_n)./sqrt(etta))))+(exp(C2_p).*hermiteH(m2+j2,((-1i.*beta_p)./sqrt(etta))));
e2=(exp(C1_p).*hermiteH(m2+j2,((1i.*beta_p)./sqrt(etta))))+(exp(C2_n).*hermiteH(m2+j2,((-1i.*beta_n)./sqrt(etta))));
e3=(exp(C1_p).*hermiteH(M-m2+j2,((1i.*beta_p)./sqrt(etta))))+(exp(C2_n).*hermiteH(M-m2+j2,((-1i.*beta_n)./sqrt(etta))));
e4=(exp(C1_n).*hermiteH(M-m2+j2,((1i.*beta_n)./sqrt(etta))))+(exp(C2_p).*hermiteH(M-m2+j2,((-1i.*beta_p)./sqrt(etta))));
T6=T6+nchoosek(M-m1-(2.*s2),j2).*((1./(2.*1i.*sqrt(etta))).^(M-m2+j2)).*((1./(po.^2)).^j2).*((b./2.*wo).^(M-m1-2*s2-j2)).*e1.*e2.*e3.*e4;
end
T5=T5+T6.*(((factorial(M-m1).*(-1).^s2)./(factorial(s2).*factorial(M-m1-2.*s2))).*(((2.*1i)./(delta.^(0.5))).^(M-m1-2.*s2)));
end
T4=T4+T5.*nchoosek(m1-(2.*s2),j1).*((1./(2.*1i.*sqrt(etta))).^(m2+j2)).*((1./(po.^2)).^j1)*((b./2.*wo).^(m1-2.*s1-j1));
end
T3=T3+T4.*(((factorial(m1).*(-1).^s1)./(factorial(s1).*factorial(m1-2.*s1))).*(((2.*1i)./(delta.^(0.5))).^(m1-2.*s1)));
end
T2=T2+T3*nchoosek(M,m2)*((-1i)^(M-m2));
end
T1=T1+T2*nchoosek(M,m1)*((1i)^(M-m1));
end
I0 = -real(X.*T1);
% convert to numerical double
I_numerical = double(vpa(I0))
I_numerical = 1×100
1.0e-05 * -0.0000 0.0000 0.0004 0.0027 0.0094 0.0226 0.0436 0.0714 0.1033 0.1360 0.1664 0.1923 0.2125 0.2266 0.2350 0.2384 0.2377 0.2338 0.2275 0.2195 0.2104 0.2006 0.1905 0.1803 0.1703 0.1605 0.1511 0.1422 0.1337 0.1256
% convert to a string
I_string = string(vpa(I0))
I_string = 1×100 string array
"-2.3513442633546138826998945821614e-34" "0.00000000017768380082384868154643079649795" "0.0000000044277950187004141662942042677492" "0.000000027401550721948087403272261920793" "0.000000093520612953757424838276518849312" "0.00000022644857791807447177975563712119" "0.00000043641763326032200486087406224207" "0.00000071391214671643056584448164413787" "0.0000010328495749864416744295828770275" "0.0000013598888417536909991387659688769" "0.0000016640886768795395304220191250956" "0.0000019230193735056647231157488838833" "0.0000021245695568105272572358531872613" "0.0000022656965469193178723558868509778" "0.0000023498197500621908266335835664606" "0.0000023841166614450826807093728632955" "0.000002377352985680945386882628263943" "0.0000023384109758994702233277556155249" "0.000002275435070813754242138321499391" "0.0000021954309210502855069435130003386" "0.0000021041568483754962747413253337556" "0.0000020061822227784234483100930813277" "0.0000019050269563360746898571777904438" "0.0000018033290385538056541117204466185" "0.0000017030103490179033967038167998853" "0.0000016054260804054055149202693732197" "0.000001511492148337317078193438689868" "0.0000014217899622639688093441912971871" "0.0000013366504105002512794833737386612" "0.0000012562199182538134144339350739958" "0.0000011805116380841164183576994507349" "0.0000011094446319136994827024416609375" "0.0000010428735348276823382683678575461" "0.00000098061077940499858127140088150226" "0.00000092244306813384132323872736228931" "0.0000008681434376935631193753685001094" "0.00000081747997036083068240602864794388" "0.00000077022197268178053337415051657325" "0.0000007261442537623793667412735538454" "0.00000068502998765036088195417091646537" "0.00000064667252902510498674772388748674" "0.0000006108764622664474392020506299252" "0.000000577458095425809535195370872914" "0.00000054624555815446189875351623814844" "0.00000051707862263150540593178110275353" "0.00000048980833611718455154902686954617" "0.00000046429653070016325568053366461742" "0.00000044041525837750506889714748771812" "0.00000041804618646732624423793210222122" "0.00000039707997848090488261524640853992" "0.00000037741567818841389556738163291566" "0.0000003589601091010320904544753940369" "0.00000034162729750501489238146419384577" "0.00000032533792417246032121665043606712" "0.00000031001880767442687644919959665933" "0.00000029560242063371977164401103856853" "0.00000028202643912440166898988550358912" "0.00000026923332463719649749060506827812" "0.00000025716993749669858945052548943797" "0.0000002457871802713516611036506040292" "0.00000023503966951034562443365022302537" "0.00000022488543403516119194315527627079" "0.00000021528563797898950026654855746217" "0.00000020620432678343844807856594429491" "0.00000019760819441284781648566162167725" "0.00000018946637012044075698723619256349" "0.00000018175022318869163305316536446353" "0.00000017443318416238508181756303020883" "0.00000016749058119225941508378247387025" "0.00000016089949020662635484752885961147" "0.00000015463859772571639834931705523203" "0.00000014868807522710376663987396023582" "0.00000014302946405957229951366747313372" "0.00000013764556998649147900231289544107" "0.00000013252036651803076914310868944178" "0.00000012763890626418041120139818019453" "0.00000012298723960771361405598898542168" "0.00000011855234005806837730453184990605" "0.00000011432203570390911758155711317207" "0.00000011028494623412255144200592304654" "0.00000010643042504450786069056990294841" "0.00000010274850599079188778599489583078" "0.000000099229854388088302459862684570289" "0.000000095865721892886925572229176216248" "0.000000092647904936359461898672293001744" "0.00000008956870640748293450062853528667" "0.000000086620900311497913473233644044255" "0.000000083797699153730146191987139238104" "0.000000081092723821068073507664666568918" "0.000000078499975753610513644188782141179" "0.000000076013811217340003668275147483607" "0.000000073628917505339643865862123666617" "0.000000071340290910194275967564189148625" "0.000000069143216323957002710745409483831" "0.000000067033248334534301368625313233845" "0.000000065006193698682151421998452094216" "0.000000063058095082102217727645456674712" "0.000000061185215966501974335860081152303" "0.000000059384026631992743700433323164034" "0.000000057651191130955178689321002367898"
% write numercial data to xls
writematrix(I_numerical,"normal_double.xls")
% write string data to xls
writematrix(I_string,"normal_string.xls")

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by