Save variable in workspace but remove from memory?

Hi folks,
I've managed to loop my function to (correctly) determine the distance travelled by each line through the grid cells it intersects. It is as follows:
if true
for Z = 1:length(ev_longitude)
x1 = -4:10; %Grid x-range
A = min(ev_longitude(Z),st_longitude(Z)); %Determine min x coordinate of line
B = max(ev_longitude(Z),st_longitude(Z)); %Determine max x coordiante of line
x = [A B]; %x-range of line
y1 = 56:63; %Grid y-range
C = min(ev_latitude(Z),st_latitude(Z)); %Determine min y coordinate of line
D = max(ev_latitude(Z),st_latitude(Z)); %Determine max y coordinate of line
y = [C D]; %y-range of line
lxmb = @(x,mb) mb(1).*x + mb(2); %Line equation: y = m*x+b
coefficients = polyfit([A, B], [C, D], 1); %Determine line coefficients
m = coefficients (1); %Gradient
b = coefficients (2); %y-value intercept
mb = [m b]; %Matrix of [slope intercept] values
L1 = lxmb(x,mb); %Calculate Line #1 = y(x,m,b)
start_of_line = [A C]; %Start of line
end_of_line = [B D]; %End of line
hix = @(y,mb) [(y1-mb(2))./mb(1); y1]; %Calculate horizontal intercepts
vix = @(x,mb) [x1; lxmb(x1,mb)]; %Calculate vertical intercepts
hrz = hix(x(1:end),mb)'; %[X Y] Matrix of horizontal intercepts
vrt = vix(y(1:end),mb)'; %[X Y] Matrix of vertical intercepts
hvix = [start_of_line; hrz; vrt; end_of_line]; %Concatanated ‘hrz’ and ‘vrt’ arrays
srtd = unique(hvix,'rows'); %Remove repeats and sort ascending by ‘x’
Longitude_Values = srtd(:,1);
Latitude_Values = srtd(:,2);
i = find((Latitude_Values<=D) & (Latitude_Values>=C)); %Remove values beyond the drawn line
Final_Latitude_Values=Latitude_Values(i); %Remove values beyond the drawn line
ii = find((Longitude_Values<=B) & (Longitude_Values>=A)); %Remove values beyond the drawn line
Final_Longitude_Values=Longitude_Values(i); %Remove values beyond the drawn line
for j = 1:length(Final_Latitude_Values)-1 %Determine the distance per each segment
Segment_Distances(j) = sqrt((Final_Longitude_Values(j+1)-Final_Longitude_Values(j))^2 + (Final_Latitude_Values(j+1)-Final_Latitude_Values(j))^2) %euclidean distance in degrees.
end
clearvars -except ev_latitude ev_longitude st_latitude st_longitude
end
end
I want to save the variable Segment_Distances, but unless I clear it from the memory using the clearvars function then it remains in memory and is used incorrectly by the function to calculate the next line's Segment_Distances variable.
I essentially need a way to store the values for Segment_Distances created by the loop, but for the previous values of Segment_Distances to not be incorporated in the next loop.
If anyone can suggest an amendment to this it would be much appreciated.The only data needed is ev_latitude, ev_longitude, st_latitude and st_longitude. These are column vectors of random numbers.
Thanks.

 Accepted Answer

Since ‘Segment_Distances’ appears to be a scalar, the easiest way would likely be to give it a second subscript:
Segment_Distances(j,k) = ...
then increment ‘k’ in the next loop.

7 Comments

Thanks, could you elaborate what you mean by increment k in the next loop?
As always, my pleasure.
You mentioned: ‘I essentially need a way to store the values for Segment_Distances created by the loop, but for the previous values of Segment_Distances to not be incorporated in the next loop.’
so I was referring to your use of ‘next loop’ there.
I did not see any outer loop, so I am not certain how your entire code works.
Sorry my bad. By the next loop I am referring to that same code shown above repeating itself again. I want this code above to repeat via the loop I have created, and to store the Segment_Distances, but to not keep them in the memory.
If I do not clear the Segment_Distances it provides the wrong values for the other Segment_Distances values, so I want to store them in the workspace but clear them from the MATLAB memory.
Thanks

"so I want to store them in the workspace but clear them from the MATLAB memory."

The workspace is stored in MATLAB memory, so this request is a contradiction in terms.

If you want to store them in a ‘.mat’ file, use the save (link) function. There are options to append new values. Then use load (link) to restore them.

I still believe it’s easier to just subscript them.

Thanks, I'll go with the subscript option. Could you elaborate on this subscript method? Do you mean as in simply add a loop to the whole script with k as the subscript, and also add this k into the Segment_Distances(j) = ... line as well?
As always, my pleasure.
‘Do you mean as in simply add a loop to the whole script with k as the subscript, and also add this k into the Segment_Distances(j) = ... line as well?’
Yes.
A similar option not involving a 2D matrix for ‘Segment_Distances’:
for j = 1:length(Final_Latitude_Values)-1 %Determine the distance per each segment
Segment_Distances(j) = sqrt((Final_Longitude_Values(j+1)-Final_Longitude_Values(j))^2 + (Final_Latitude_Values(j+1)-Final_Latitude_Values(j))^2) %euclidean distance in degrees.
end
Segment_Distancev(k) = Segment_Distances;
creating a new vector, ‘Segment_Distancev’.
If you want to keep only the last value computed for ‘Segment_Distances’, this will likely be more efficient.
If you want to keep all of them, use the 2D matrix option:
Segment_Distances(j,k) = ...

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

BOB
on 12 Sep 2018

Commented:

on 13 Sep 2018

Community Treasure Hunt

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

Start Hunting!