Problem with data indexing in nested structure

3 ビュー (過去 30 日間)
Shuo Zhang
Shuo Zhang 2019 年 7 月 16 日
コメント済み: Shuo Zhang 2019 年 7 月 19 日
Hi everyone,
I have a nested structure(see attacted file egc.mat), and I'd like to store value based on their 'source', as defined in the structure, if it exists. Otherwise use NaN
As you can see, not all of the sources are available for each row, for example, in Z.hours(2).AA, source 'ui' is missing
Also note that the order of source will change, for example, in Z.hours(1).AA, source 'sg' and its respective value are in the first row, while in Z.hours(2).AA, they are in the second row.
In the end, I want things in a matrix, so something like this,
sg=[1 3.25;
2 2.601;
3 0.1273];
ns=[1 4.07;
2 2.6521;
3 2.8242];
mm=[1 4.01;
2 1.3728;
3 0.6847];
ui=[1 3.56;
2 NaN;
3 2.6219];
I tried the following command
sg=zeros(3,2);
for i=1:3
sg(i,1)=i;
sg(i,2)=Z.hours(i).AA(source=='sg')
end
It doesn't work because I didn't access the data correctly.
So is there any good solution for that?
Thank you

採用された回答

Bruno Luong
Bruno Luong 2019 年 7 月 17 日
編集済み: Bruno Luong 2019 年 7 月 17 日
n = length(Z.hours); % 3
sg=zeros(n,2);
for i=1:n
b = strcmp({Z.hours(i).AA(:).source},'sg');
sg(i,:) = [i,Z.hours(i).AA(b).value]; % suppose exactly one match
end
  3 件のコメント
Bruno Luong
Bruno Luong 2019 年 7 月 17 日
編集済み: Bruno Luong 2019 年 7 月 17 日
Well that is the reason I commented my code with "suppose exactly one match" so I must say what you report is not really a surprise to me.
But when you ask for "any fixes?" you must first specify what output array row should contain in case of no match (and it's quite easy to check if there is no match - hint using sum(b) or any(b) - and why can't you try to change the code on your own?).
Idem if there is more than one matches of inner struct, the assigment command will crash with an error. Again I don't know how you want the code to behave in this case.
Shuo Zhang
Shuo Zhang 2019 年 7 月 19 日
I see. I'll find a better solution

サインインしてコメントする。

その他の回答 (1 件)

Bob Thompson
Bob Thompson 2019 年 7 月 16 日
In my experience, nested structures do a really good job of intuitively organizing data, but are difficult to work with in matlab.
Are you getting a specific error message? If so, please post it in it's entirety.
'It doesn't work because I didn't access the data correctly.'
I don't know exactly what this means, but I'm going to hazard a guess and say the error message gave you something along the lines of, 'function was looking for one result, but returned three instead.' You can try getting around this using a find function, but I can't guarentee it will work. Others on here can give a more technical explanation of why nested loop indices generally need to be pretty specific. In short, you cannot make a call like AA(:).source because the computer actually sees AA(1), AA(2), AA(3),..., AA(n).source which doesn't really make any sense to it. You can sometimes use [] to get around this, but that is usually only successful for the final layer, not an intermediary.
loc = find([Z.hours(i).AA(:)]=='sg'); % Almost certain this won't produce anything
  2 件のコメント
Shuo Zhang
Shuo Zhang 2019 年 7 月 17 日
Hi Bob,
the error says Undefined function or variable 'source'.
if I put source in quotation marks, then the error is Matrix dimensions must agree.
Bob Thompson
Bob Thompson 2019 年 7 月 17 日
I'm assuming you have source defined as part of the Z structure. Using (source == 'sg') is not going to work because source is not defined as a variable, but as a field of the variable Z.
Using 'source' == 'sg' is not going to work because you are comparing the two strings 'source' and 'sg' which will return the matrix dimension error because one has six elements and one has two elements.
Again, I think you're going to have better luck looking into find.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by