Analysing mutiple structures of data

Hi,
I am fairly new to the MATLAB environment so please try to tolerate my incompetence :)
I have about 600 data sets of casts that measure water temperature and salinity through depth at different times in struct format.
I have written a script that calculates freezing temperature, depth, what depth freezing temperature and water temperature meet etc and it works fine on a single struct when loaded into the workspace.
But I want to analyse all 600 data sets and create a vector of depths where freezing point and temperature meet so I can plot this over time.
I have little to no programming experience but I'm thinking I can achieve this with a for or loop statement?
Any insight or help would be appreciated.
Cheers,
Ben

댓글 수: 8

Ben
Ben 2011년 12월 13일
Hi,
Still a bit confused. I think the main thing that is confusing me is the "struct" format for the data. Each cast is in MATLAB format but when I open a cast each struct had the same. I took a screenshot to show what I mean: http://tinypic.com/r/2a4rbsy/5
It shows my script 'maxdepth' working on a single struct 'ctd' but I am not sure how to make it work on all the casts and then give me a vector of maxdepth and starttime without going through each one manually.
When I tried a for loop I couldn't get it to load anything.
Any ideas?
Cheers,
Ben
Fangjun Jiang
Fangjun Jiang 2011년 12월 14일
See update.
Ben
Ben 2011년 12월 14일
Hi,
Sorry I got sidetracked reading other ways to achieve what I am doing so I didn't see yours but I will also try it. This is how I have managed to do it.
% Loading data
clear all
files = dir('*.mat');
for i=3:length(files)
load(files(i).name)
% Setting variables
T = ctd.temperature;
S = ctd.salinity;
p = ctd.pressure;
Tp = ctd.pot_temp;
lat = str2double(ctd.latitude(6:11));
datetime = ctd.date_time;
% Calculating variables
X = (sind(lat/57.29578))^2;
gvar = 9.780318 * (1.0 + (5.2788e-3 + 2.36e-5 .* X) .* X) + 1.092e-6 .* p;
depth =((((-1.82e-15 .* p + 2.279e-10) .* p -2.2512e-5) .* p + 9.71659) .* p)./gvar;
Tf = fp(S,p);
% Max depth of supercooling
K = find(diff(sign(T-Tf))>0);
if K ~= 0
Tint = [T(K) T(K+1)];
Tfint = [Tf(K) Tf(K+1)];
Dint = [depth(K) depth(K+1)];
[x y] = curveintersect(Tint,Dint,Tfint,Dint);
Maxdepth = max(y);
Starttime = datenum(datetime);
elseif isempty(K) == 1
Maxdepth = 0;
Starttime = datenum(datetime);
end
end
The only trouble I am having now is that I am not sure how to get the vector of maxdepths. I know it is calculating them all because when I unsurpress the outputs it goes through and calculates them all.
Cheers,
Ben
Fangjun Jiang
Fangjun Jiang 2011년 12월 14일
That's good. You just need to pre-allocate the Maxdepth and then use Maxdepth(i) whereever you use Maxdepth.
N=length(files)-2;
Maxdepth=zeros(N,1);
for i=1:N
...
MaxDepth(i)=...
end
Ben
Ben 2011년 12월 14일
Hi, thanks for your answers btw, very helpful.
So I did what you suggested (I think):
N = length(files)-2;
maxdepth = zeros(N,1);
for i=1:N
K = find(diff(sign(T-Tf))>0);
if K ~= 0
Tint = [T(K) T(K+1)];
Tfint = [Tf(K) Tf(K+1)];
Dint = [depth(K) depth(K+1)];
[x y] = curveintersect(Tint,Dint,Tfint,Dint);
maxdepth(i) = max(y);
elseif isempty(K) == 1
maxdepth(i) = 0;
end
end
Now it seems to take a very long time to process the information (so long I have not been able to get an answer)
Do I have my code mixed up a bit? Can't seem to work out what I have done wrong.
Cheers,
Ben
Fangjun Jiang
Fangjun Jiang 2011년 12월 14일
It looks good for me, although I don't understand the rest of the code, for example, curveintersect(). You can use profile() to see where is the bottle neck.
Ben
Ben 2011년 12월 14일
I think I know what it is doing but not sure why,
It is writing each maxdepth for each one of the files onto the vector maxdepth over and over so by the end I end up with a vector which is (N,1) but full of the last value of maxdepth.
curveintersect is just my way of finding where T meets Tf
I'm working on it but I don't know why it isn't working
Thanks though,
Ben
Ben
Ben 2011년 12월 14일
Here is my entire code. Currently it is writing (N x 1) sized vectors of the maxdepth of 1st data file (i.e the same value N times) then replacing it with (N x 1) sized zero matrices then moving onto the next data file. Therefore at the end I am just left with a (N x 1) sized vector of the maxdepth last data file.
I think my for loops must be muddled up or something?
Can you see any obvious mistakes?
clear all
files = dir('*.mat');
for i=3:length(files)
load(files(i).name)
% Setting variables
T = ctd.temperature;
S = ctd.salinity;
p = ctd.pressure;
Tp = ctd.pot_temp;
lat = str2double(ctd.latitude(6:11));
datetime = ctd.date_time;
% Calculating variables
X = (sind(lat/57.29578))^2;
gvar = 9.780318 * (1.0 + (5.2788e-3 + 2.36e-5 .* X) .* X) + 1.092e-6 .* p;
depth =((((-1.82e-15 .* p + 2.279e-10) .* p -2.2512e-5) .* p + 9.71659) .* p)./gvar;
Tf = fp(S,p);
% Max depth of supercooling
N = length(files) - 2;
maxdepth = zeros(N,1);
for k = 1:N;
K = find(diff(sign(T-Tf))>0);
if K ~= 0
Tint = [T(K) T(K+1)];
Tfint = [Tf(K) Tf(K+1)];
Dint = [depth(K) depth(K+1)];
[x y] = curveintersect(Tint,Dint,Tfint,Dint);
maxdepth(k) = max(y);
elseif isempty(K) == 1
maxdepth(k) = 0;
end
end
end

댓글을 달려면 로그인하십시오.

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 12월 13일

0 개 추천

A for-loop sounds good for your use case. If you provide a small example data, some might be able to help you figure out a way without the for-loop. But many examples have prove that using for-loop in new version of MATLAB is faster. Anyway, try it first. It's usually straightforward and it's easy to understand. If you have problems, show your code and people will help you.
If your function maxdepth(ctd) works fine, then what is your 600 data sets look like? Is it a struct array?
Try this to understand what is struct array. Use your ctd variable.
S_data=ctd; %make a copy
S_data(600).cast_number='600';
whos S_Data
It will tell S_Data is 1x600 struct, each one S_data(1), S_data(2),..., S_data(600) is a structure like ctd, you can do a for-loop
max_depth=zeros(600,1);
start_time=zeros(600,1);
for k=1:600
[max_depth(k),start_time(k)]=maxdepth(S_data(k));
end
Now I noticed your .mat file, I think you need this:
max_depth=zeros(600,1);
start_time=zeros(600,1);
for k=1:600
MatFile=sprintf('cast%3d.mat',k);
ctd=load(MatFile);
[max_depth(k),start_time(k)]=maxdepth(ctd);
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Downloads에 대해 자세히 알아보기

제품

질문:

Ben
2011년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by