Save variable from each run through of a loop into .mat file?

조회 수: 5 (최근 30일)
BOB
BOB 2018년 9월 14일
답변: KSSV 2018년 9월 14일
Hi folks,
I have my looped function
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
Final_Segment_Distance_Per_Travel_Path = Segment_Distances(1:end) %Final segment per travel path
save('End_Data','Final_Segment_Distance_Per_Travel_Path')
clearvars -except ev_latitude ev_longitude st_latitude st_longitude
end
end
It produces a variable called Final_Segment_Distance_Per_Travel_Path. I want to save this value each time to a .mat file using the save function, without the previous value being overwritten each time the loop runs.
Thanks for any help.

채택된 답변

KSSV
KSSV 2018년 9월 14일
Check this demo code:
n = 10 ; % loop count
filename='test.mat';
File = matfile(filename, 'Writable', true);
for i = 1:n
SE = rand(1,2) ;
DE = rand(1,3) ;
% save variables to file
File.SE(i,1:2) = SE ;
File.DE(i,1:3) = DE ;
end
clear File;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Other Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by