How can I make this code a function to extract data and use the data in another code?
Afficher commentaires plus anciens
INPUT.Out.PATH_save=('./trial_test_Auswertung_2016/');
INPUT.Out.PATH_data=('./temp/');
INPUT.IN={... %file Type | file | Variable name | Location (Str ox coords) | %Location | Variable | Unit | Prestr | Poststr | VariableLimits | LoadTempData ? y=1
'.nc', './links/out.l.PIZhs01.2D.nc', 'Mesh0_node_Wasserstand_2d' , 'ALW', 'Pegel ALW' 'Wasserstand', 'm NHN', '', ' ', [-4,4], 1;...
'.nc', './links/2D.a.out.HJ2016.v01.nc', 'Mesh2_face_Wasserstand_2d' ,[428500,5977000], 'Modellrand' 'Wasserstand', 'm NHN', '', ' seeseitig', [-4,4], 1;...
'.nc', './links/2D.a.out.HJ2016.v01.nc', 'Mesh2_face_Salzgehalt_2d' ,[428500,5977000], 'Modellrand' 'Salzgehalt', 'g/kg', '', ' (2D) seeseitig', [30,35], 1;...
'.nc', './links/2D.a.out.HJ2016.v01.nc', 'Mesh2_face_Temperatur_2d' ,[428500,5977000], 'Modellrand' 'Wassertemperatur', '°C', '', ' (2D) seeseitig', [0,30], 1 ;...
'davit.dat','./davit_data/2D.a.unk.2016-2015_SigWellenhoehe_GridPoint_10926_ALW.dat','Davit_SigWellenhoehe' , 'ALW' ,'Pegel ALW', 'Signifikante Wellenhöhe', 'm', '', '', [0,5], 0; ... '
};
% % %--------------------------------------------------------------------------
%Input-Loop
for f=1:size(INPUT.IN,1)
%READ
% temp variables
FileTyp=INPUT.IN{f,1};
File= INPUT.IN{f,2};
Variable = INPUT.IN{f,3};
Location = INPUT.IN{f,4};
LocationFileStr=strrep([num2str([Location])],' ','_');
LocationPlotStr=strrep([num2str([Location])],' ',',');
LocationName = INPUT.IN{f,5};
VariableStr = INPUT.IN{f,6};
UnitStr = INPUT.IN{f,7};
PreStr = INPUT.IN{f,8};
PostStr= INPUT.IN{f,9};
VariablePotLimits= INPUT.IN{f,10};
LoadTemp= INPUT.IN{f,11};
LoadTempPath=(INPUT.Out.PATH_data);
mkdir(LoadTempPath);
LoadTempStr=[Variable '_' LocationFileStr '_' LocationName];
%read cases
switch INPUT.IN{f,1}
case '.nc'
if LoadTemp==1
load([LoadTempPath LoadTempStr])
DATA(f)=DATA_temp;
else
if f==1 | ~exist('nc')
nc = NCLib(File);
elseif f>1 & ~strcmp(INPUT.IN{f,2},INPUT.IN{f-1,2})==1
clear nc
nc = NCLib(File);
end
if ischar(Location)
[val,dd,aux,meta] = nc.readTimeseries(Variable,'stations',Location);
elseif sum(isnumeric(Location))>0
[val,dd,aux,meta] = nc.readTimeseries(Variable,'coordinates_xy',Location);
else
error('undefined Station type')
end
DATA(f).edit.Time = datenum(aux.time(:));
DATA(f).edit.Values = val(:);
DATA(f).meta = meta;
DATA(f).aux= aux;
%save temp
DATA_temp=DATA(f);
save([LoadTempPath LoadTempStr '.mat'], 'DATA_temp','-mat'); clear DATA_temp
if aux.distance > 1000 % in m
warning('Deviation from defined coordinates > 1km!')
end
end
case 'davit.dat'
% File_temp=INPUT.IN.Files{f};
temp= readtable(File, 'Delimiter', ' ');
DATA(f).edit.Time = datenum(temp{:,1});
DATA(f).edit.Values = temp{:,2};
otherwise
warning('No defined input type')
end
Réponse acceptée
Plus de réponses (1)
Cris LaPierre
le 3 Jan 2024
1 vote
I would use the refactor button. See here: https://www.mathworks.com/videos/convert-code-to-reusable-functions-using-the-refactor-button-in-matlab-1680849938557.html
2 commentaires
janas
le 3 Jan 2024
Cris LaPierre
le 3 Jan 2024
Which lines of code are you wanting to be a separate function? And what data should it return?
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!