any help in Matlab code

Hello, eveyone i hope you doing well
i have enquier regarding with my code
lets say i have code with 10 lines
in line 5 i saved some parameters as a save ('data.mat')
then loaded in another file1.m to used
then the results that i generated from file1.m also save it
now i need to use these results in original file in line 7
and contiune ruuning the program.
Is this possible to do in Matlab like pause the program and go to file1.m to run and then reassume the running in original code?
thanks alot in advance

 采纳的回答

Ali - if you use functions then you can call a function from your main program (whether this is a script or function itself) and have it return something to use in that main program. So from your example,
function myMainProgram
% do something for lines 1-4
% save to 'data.mat' file
% call the external function that returns some data
[results] = myOtherFunction();
% use results in remainder of the code
If you are saving data to a mat-file only so that your other code can use it, then use functions so that you can pass data into and receive data from them. See the examples from the link I've included in this answer.

5 个评论

thank you so much for your answering Geoff
i dont know if i can use this approch due to i have some process between line 1-4 are releated with the code after line 4 so can i seprate it by using function and called again ?
Ali - it is okay to call the function at line 5. Anything that happens from line 6 and after will still be "aware" of what happend at lines through four. Please post some of your code if you are unsure.
options=odeset('RelTol',1e-7,'AbsTol',1e-7);
tmax=6000;
dicretization=1;
max_iter=2;
u=1*ones(1,tmax+1);
tspan1=0:dicretization:tmax;
tspan2=tmax:-dicretization:0;
W=rand(1,2)-0.5; TH=(rand(1,1)-0.5);
w1=W(1); w2=W(2); th1=TH(1);
n1 = (w1*x1 + w2*x2)-th1;
Winit=[W TH];
for j=1:max_iter
[T,Y]=ode15s(@(t,y) ODEsimple(t,y,u,x1,x2,yd),tspan1,[Winit],options); %% solving ODE to obtain y
x1t=[T']; w1=Y(:,1); w2=Y(:,2); th1=Y(:,7);
W_TH=[w1(end) w2(end)];
so, now i need to used W_TH data in another file to run and the results that i generated are (Lem1 Lem2) i want to use it below and then reassume running the program
finalcostate=[Lem1 Lem2 ];
[T1,cox]=ode15s(@(t,y1)...
Costatesimple(t,y1,u,x1,x2,x11,x12,x13,x14,x21,x22,x23,x24,yd1,yd2,yd3,yd4,x1t,yd,...
w1,w2,w3,w4,v1,v2,th1,th2,th3),tspan2,[finalcostate],options);
what i need is that firstly, run the program to generat W_TH
secondly, use W_TH in another file to get [ Lem1 Lem2]
finaly, reassume the original program again.
How would i do that ?
I appreciate your time to assist me.
Ali - I'm not sure where your for loop ends (in the first block of code) so am assuming that on each iteration of your loop, you will pass W_TH to this other function and get back Lem1 and Lem2. This means that your other function needs to be defined as
function [Lem1,Lem2] = myOtherFunction(W_TH)
% do work here
end
The above is saved to a file named myOtherFunction.m (the function can be called whatever you want but the function name and name of the file need to be identical). Then you call this function from your code as
options=odeset('RelTol',1e-7,'AbsTol',1e-7);
tmax=6000;
dicretization=1;
max_iter=2;
u=1*ones(1,tmax+1);
tspan1=0:dicretization:tmax;
tspan2=tmax:-dicretization:0;
W=rand(1,2)-0.5; TH=(rand(1,1)-0.5);
w1=W(1); w2=W(2); th1=TH(1);
n1 = (w1*x1 + w2*x2)-th1;
Winit=[W TH];
for j=1:max_iter
[T,Y]=ode15s(@(t,y) ODEsimple(t,y,u,x1,x2,yd),tspan1,[Winit],options); %% solving ODE to obtain y
x1t=[T']; w1=Y(:,1); w2=Y(:,2); th1=Y(:,7);
W_TH=[w1(end) w2(end)];
[Lem1,Lem2] = myOtherFunction(W_TH); % <--- call this function here
finalcostate=[Lem1 Lem2 ];
[T1,cox]=ode15s(@(t,y1)...
Costatesimple(t,y1,u,x1,x2,x11,x12,x13,x14,x21,x22,x23,x24,yd1,yd2,yd3,yd4,x1t,yd,...
w1,w2,w3,w4,v1,v2,th1,th2,th3),tspan2,[finalcostate],options);
% etc.
end
Thank you so much geoff it works

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by