Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to save the data of x (in my folowing case) to an Excel file after each loop .

1 回表示 (過去 30 日間)
Phuc Deo
Phuc Deo 2019 年 8 月 2 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I would like to save the data of x after each loop to separate column . for i =1 colums A, for i=2 colums B and so on .here is my code thank y'all
function [x,y]=trajectory1
A=linspace(-100*10^-10 ,100*10^-10,1000);
for i=1:length(A)
x0=A(i);
ydot0=10^6;
L1=-1*10^-10;
t0=0;tf=12;xdot0=0;
a=L1/2;
tspan=[t0 tf]
IC=[x0,xdot0,L1,ydot0];
sdot = @(t,s) [s(2);-256*(s(1)+a)/((s(1)+a)^2+s(3)^2)^1.5-256*(s(1)-a)/((s(1)-a)^2+s(3)^2)^1.5;
s(4);-256*s(3)/((s(1)+a)^2+s(3)^2)^1.5-256*s(3)/((s(1)-a)^2+s(3)^2)^1.5];
options=odeset('Events',@event1s)
[time,state_values]=ode45(sdot,tspan,IC,options);
x=state_values(:,1);
xdot=state_values(:,2);
y=state_values(:,3);
ydot=state_values(:,4);
figure(i)
plot(x,y);
xlswrite('data222.xls',[x]);
end
end
and the code for events
function [check, isterminal, direction] =event1s(t,s)
direction=0;
isterminal=1;
check = s(3)>=0.5*10^-10;
end
  4 件のコメント
dpb
dpb 2019 年 8 月 2 日
Nothing subtle, no... :)
I often start with idea that initially seems like just a comment and while typing it ends up morphing into what may stand as an Answer... :) Occasionally I'll move it; generally I'd say that unless it included actual code I'd probably as here, just leave it go...
But, I can see the point you raise...in part here I figured somebody else would shortly be along and actually write code while I was being lazy...
Jon
Jon 2019 年 8 月 5 日
Thanks for your response -makes sense

回答 (1 件)

Neuropragmatist
Neuropragmatist 2019 年 8 月 5 日
help xlswrite and look at the 'range' option.
M.
  2 件のコメント
Phuc Deo
Phuc Deo 2019 年 8 月 7 日
how can i do that?
Neuropragmatist
Neuropragmatist 2019 年 8 月 8 日
Go to your Matlab and type:
help xlswrite
and then read what it says, paying particular attention to the section on using the 'range' option. Or do the same on this webpage:
If you have writetable available you should use that instead as dpb suggested:
The code you want might look something like this:
tablename = 'mytable.xlsx';
T1 = table(['M';'F';'M'],[45 45;41 32;40 34],{'NY';'CA';'MA'},[true;false;false]);
writetable(T1,tablename);
T2 = table(['K';'L';'P'],[55 55;51 52;50 54],{'KK';'PP';'MM'},[true;true;true]);
writetable(T2,tablename,'Range',['A' num2str(size(T1,1)+2)],'WriteVariableNames',false);

Community Treasure Hunt

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

Start Hunting!

Translated by