any help in Matlab code
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
Geoff Hayes
il 23 Apr 2020
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 Commenti
Geoff Hayes
il 24 Apr 2020
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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Entering Commands in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!