How to extract specific data from multiple .mat files

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

3 commentaires

You are overwriting your variables every iteration of k. Is that intentional?
The cause of the error is that you are trying to store some value in a scalar array:
mjd_KATH(ip) = x_.zwd(k).mjd;
%^^^^^^^^ ^^^^^^^^^^^^^
%|||||||| this is apparently not a scalar
% ip is scalar, so this is a scalar
Hi Rik, Thanks for your reply. "k" is number of stations. In 134 files, every stations are not listed in order, but randomly. For example, in the first file, station "KATH" can be listed at third place; however, in the second file, this station can be listed at fifth place, etc. That's why I use "k".
If I dont use "ip", but use "mjd_KATH = x_.zwd(k).mjd", I can get 24 "x_.zwd(k).mjd" values but the values only for 1 mat file. I need to get "x_.zwd(k).mjd" values from 134 files.
If I use "mjd_KATH(ip) = x_.zwd(k).mjd(ip)", Matlab shows "Index exceeds the number of array elements (11)."
I am struggling with how to extract values from all 134 files. Thanks.
Stephen23
Stephen23 le 18 Mai 2020
Modifié(e) : Stephen23 le 18 Mai 2020
Rather than storing the data in an array using indices you should consider using either a structure or a table: these would let you allocate the imported data using the station name, and then the name order in the files is totally irrelevant. It would not require copy-and-paste code like all of those ifs, and it would be simple to access your data too.
If you upload some sample files I (or someone else) can show you how.

Connectez-vous pour commenter.

 Réponse acceptée

Rik
Rik le 18 Mai 2020
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

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by