Creating a loop that performs actions for each set of values linked to a unique (but random) ID
Mostra commenti meno recenti
I have a csv file containing CTD data with columns for: Cast ID (cast); latitude; longitude; depth; salinity; temperature. The objective is to create sound speed profiles for each unique cast. Each cast has a varying number of records attached to it (i.e. a cast may have measurements at 15 different depths, and the next may have 7 measurements). I would like to create a loop for this, but I'm having trouble with:
- How to pass the loop from the first cast to the second etc - the cast IDs are effectively random, i.e. they do not change with any pattern.
- How to ensure that the only data being used in each sound speed calculation is only associated with that individual cast.
Verbiosly, the script would be along the lines of:
for each unique cast ID
run sound speed profile equation
output plot
end
With the loop repeating for each cast ID.
Is anyone able to point me in the direction of how to format the loop so it runs for each unique but random ID, and how to ensure that each sound speed profile is only calculated based on records associated with that unique ID? Thanks!
2 Commenti
Mathieu NOE
il 30 Ott 2020
hello
have you already written the code ? are you willing to share it ?
also if you can share a csv input data file, that would help
Aryn Sanojca
il 3 Nov 2020
Risposte (1)
Alan Stevens
il 3 Nov 2020
Does this help:
%% Setup the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 9);
% Specify range and delimiter
opts.DataLines = [1, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["cast1", "Var2", "Var3", "Var4", "Var5", "Var6", "depth", "temp", "sal"];
opts.SelectedVariableNames = ["cast1", "depth", "temp", "sal"];
opts.VariableTypes = ["double", "string", "string", "string", "string", "string", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["Var2", "Var3", "Var4", "Var5", "Var6"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var2", "Var3", "Var4", "Var5", "Var6"], "EmptyFieldRule", "auto");
% Import the data
tbl = readtable("CTD_Cleaned.csv", opts);
%% Convert to output type
cast1 = tbl.cast1;
depth = tbl.depth;
temp = tbl.temp;
sal = tbl.sal;
%% Clear temporary variables
clear opts tbl
% All the above comes from MATLAB's Import Data wizard
%% Find unique IDs by looking for zero depths
ix = find(depth==0);
ID = cast1(ix);
%% Find corresponding depths, temperatures and salinities
for i = 1:numel(ix)-1
p1 = ix(i); p2 = ix(i+1)-1;
dpth = depth(p1:p2);
T = temp(p1:p2);
saln = sal(p1:p2);
% Now put your sound speed profile calculation here for ID(i)
end
Categorie
Scopri di più su Audio and Video Data in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!