How to temporarily stop a script, extract data, and then continue?

8 visualizzazioni (ultimi 30 giorni)
How can I temporarily pause a for loop, extract data at that point, and then continue the script? Here is my code:
A = importdata('HW8.2.Custom1.txt','%f%f%f', ' ',1);
n=length(A);
for i = 1:2:n-1
line_desc = A{i};
line_data = str2num(A{i+1});
if strcmp(line_desc,'inlet');
P_in = line_data(1);
z_in = line_data(2);
D_in = line_data(3);
elseif strcmp(line_desc,'exit');
P_out = line_data(1);
z_out = line_data(2);
D_out = line_data(3);
elseif strcmp(line_desc,'fitting');
K_L = line_data(1);
V = line_data(2);
Element_Num = line_data(3);
elseif strcmp(line_desc,'pipe');
L = line_data(1);
D = line_data(2);
rough = line_data(3);
delta_z = line_data(4);
Element_Num = line_data(5);
elseif strcmp(line_desc,'pump');
H_s = line_data(1);
Element_Num = line_data(2);
else strcmp(line_desc,'turbine');
H_s = line_data(1);
Element_Num = line_data(2);
end
end
What I am trying to do is stop the for loop each i, make variables based on that i, and then continue the loop and repeat for each iteration. Right now, however, it is just running the whole for loop and then giving me variables based on the final iteration (when i = n-1) Is it possible to pause it after each iteration and what function would I use?

Risposta accettata

Jan
Jan il 25 Apr 2017
Modificato: Jan il 25 Apr 2017
What about storing the values in vectors? Then there is no need to stop the loop:
n = length(A);
P_in = zeros(1, n); % Pre-allocate
z_in = zeros(1, n);
D_in = zeros(1, n);
inlet_i = 0;
for i = 1:2:n-1
line_desc = A{i};
line_data = str2num(A{i+1});
switch line_desc
case 'inlet'
inlet_i = inlet_i + 1;
P_in(inlet_i) = line_data(1);
z_in(inlet_i) = line_data(2);
D_in(inlet_i) = line_data(3);
case ... same for the other variables
end
P_in = P_in(1:inlet_i); % Crop unused elements
z_in = z_in(1:inlet_i);
D_in = D_in(1:inlet_i);
Stopping scripts to manipulate the data is "meta-programming" and in consequence prone to errors and hard to debug.
  2 Commenti
Neil Solan
Neil Solan il 25 Apr 2017
Modificato: Neil Solan il 25 Apr 2017
Can you explain what the inlet_i does exactly? Does it apply to each case or only the inlet and will I have to write another one for the other cases (i.e. exit_i, pipe_i, fitting_i, etc.)? Also I tried writing this code for all the variables and when I run it I am getting a vector that still only pulls one value from the .txt file that I'm working from and just gives zeros for the rest of the values. I can't figure out why it's not giving me each value?
EDIT: I got it to work for all the values that I needed and accepted your answer, thank you very much! If you could still explain the inlet_i though to me I don't fully understand why you needed that/what purpose it serves.
Jan
Jan il 25 Apr 2017
@Neil: At first the vector P_in is allocated and filled with zeros. This is more efficient than letting it grow iteratively. The variable "inlet_i" is the cursor, which defines where to fill in the new values if the "inlet" keyword appears. A more general example:
x = zero(1, 10);
ix = 0;
for k = 1:10
if rand < 0.5
ix = ix + 1
x(ix) = k;
end
end
x = x(1:ix)
Let this run and observe what's going on in the debugger. This selects randomly 10 numbers from 1 to 10 and crops the undefined elements afterwards. Not really useful, but a demonstration :-)

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 25 Apr 2017
Why not just set a breakpoint wherever you want it to stop? Then do your stuff in the command window, then tell it to "Continue".
Tutorial on debugging:

Categorie

Scopri di più su Programming 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!

Translated by