MATLAB: How to extract specific data from multiple .mat files

extract multiple values

Hi there,
I have 134 .mat files (sessions). Each file (session) contains 10 stations. Each station contains 24 values (hourly value) for each file. I need to extract those values from 134 .mat files. I use loop, however, matlab always shows "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
if loadf==1
for ip = 1: ns % number of sessions
nstat = length(x_.antenna);
sres = std(res.mainVal);
for k = 1:nstat
stnam = x_.antenna(k).name;
if strcmp(stnam,'KATH12M ')||strcmp(stnam,'HART15M ')||strcmp(stnam,'YARRA12M')||strcmp(stnam,'HOBART26')||strcmp(stnam,'WARK12M ')||strcmp(stnam,'ONSALA60');
if strcmp(stnam,'KATH12M ')
id = 1;
mjd_KATH(ip) = x_.zwd(k).mjd;
zwd_KATH(ip) = x_.zwd(k).val;
dzwd_KATH(ip) = x_.zwd(k).mx;
end
if strcmp(stnam,'HART15M ')
id = 2;
mjd_HART(ip) = x_.zwd(k).mjd;
zwd_HART(ip) = x_.zwd(k).val;
dzwd_HART(ip) = x_.zwd(k).mx;
end
if strcmp(stnam,'YARRA12M')
id = 3;
mjd_YARRA(ip) = x_.zwd(k).mjd;
zwd_YARRA(ip) = x_.zwd(k).val;
dzwd_YARRA(ip) = x_.zwd(k).mx;
end
if strcmp(stnam,'HOBART26')
id = 4;
mjd_HOBART(ip) = x_.zwd(k).mjd;
zwd_HOBART(ip) = x_.zwd(k).val;
dzwd_HOBART(ip) = x_.zwd(k).mx;
end
if strcmp(stnam,'WARK12M ')
id = 5;
mjd_WARK(ip) = x_.zwd(k).mjd;
zwd_WARK(ip) = x_.zwd(k).val;
dzwd_WARK(ip) = x_.zwd(k).mx;
end
if strcmp(stnam,'ONSALA60')
id = 6;
mjd_ONSA(ip) = x_.zwd(k).mjd;
zwd_ONSA(ip) = x_.zwd(k).val;
dzwd_ONSA(ip) = x_.zwd(k).mx;
end
end
end
end
end

Best Answer

You need to think about how you want to store your data. The code below will store everything in a cell array, but it would probably make a lot more sense to but the data in a 2D array or something. I will leave the further processing to you. I removed the id lines, as they get overwriten and don't seem to play any role.
if loadf==1
mjd_KATH=cell(1,ns);
zwd_KATH=cell(1,ns);
dzwd_KATH=cell(1,ns);
%repeat this for the other antennas
for ip = 1: ns % number of sessions
nstat = length(x_.antenna);
sres = std(res.mainVal);
for k = 1:nstat
switch x_.antenna(k).name%whenever you have many ifs, consider using switch instead
case 'KATH12M '
mjd_KATH{ip} = x_.zwd(k).mjd;
zwd_KATH{ip} = x_.zwd(k).val;
dzwd_KATH{ip} = x_.zwd(k).mx;
case 'HART15M '
mjd_HART{ip} = x_.zwd(k).mjd;
zwd_HART{ip} = x_.zwd(k).val;
dzwd_HART{ip} = x_.zwd(k).mx;
case 'YARRA12M'
mjd_YARRA{ip} = x_.zwd(k).mjd;
zwd_YARRA{ip} = x_.zwd(k).val;
dzwd_YARRA{ip} = x_.zwd(k).mx;
case 'HOBART26'
mjd_HOBART{ip} = x_.zwd(k).mjd;
zwd_HOBART{ip} = x_.zwd(k).val;
dzwd_HOBART{ip} = x_.zwd(k).mx;
case 'WARK12M '
mjd_WARK{ip} = x_.zwd(k).mjd;
zwd_WARK{ip} = x_.zwd(k).val;
dzwd_WARK{ip} = x_.zwd(k).mx;
case 'ONSALA60'
mjd_ONSA{ip} = x_.zwd(k).mjd;
zwd_ONSA{ip} = x_.zwd(k).val;
dzwd_ONSA{ip} = x_.zwd(k).mx;
otherwise
error('unknown antenna')
end
end
end
end
Related Question