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 个评论
Rik
2020-5-18
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
Vicky Liu
2020-5-18
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!