Extracting data from multiple structures in a .mat file

Hello,
I have some data which is in nested structures names an example structure is below.
Signal_00.y_values.values
I want to extract this data using a foor loop and place it all in m x n array when m is the original structure, and n the number of points. Here's where I got to:
sigName = {'Signal_00';'Signal_01';'Signal_02';'Signal_03'...}
for k = 1:24
tData(k,:) = sigName(k).y_values.values
end
The error I recieve in command window when I try to open the data is:
>> sNames(1).y_values.values
Dot indexing is not supported for variables of this type.
What is the correct way to generate a structure path which can be used in the for loop?

3 件のコメント

KSSV
KSSV 2019 年 6 月 13 日
First of all: does this code work:
sigName = {'Signal_00';'Signal_01';'Signal_02';'Signal_03'...}
for k = 1:24
tData(k,:) = sigName(k).y_values.values
end
Stephen23
Stephen23 2019 年 6 月 13 日
編集済み: Stephen23 2019 年 6 月 13 日
@KSSV: "First of all: does this code work:"
sigName = {'Signal_00';'Signal_01';'Signal_02';'Signal_03'...}
for k = 1:24
tData(k,:) = sigName(k).y_values.values
end
if sigName is a cell array of character vectors, then sigName(k) will return a scalar cell array... and what do you expect the dot notation to achieve with a scalar cell array?
John Doe
John Doe 2019 年 6 月 13 日
Nope!

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

 採用された回答

Stephen23
Stephen23 2019 年 6 月 13 日
編集済み: Stephen23 2019 年 6 月 13 日

1 投票

Do NOT load directly in the workspace.
If you do that, then you will force yourself into writing slow, complex, buggy code by trying to access the variable names dynamically. Read this to know why this is a bad approach to writing code:
The solution is very simple: always load into an output variable (which is a scalar structure):
S = load(...)
The variables in the .mat file are the fields of that scalar structure. You can trivially and efficiently iterate over the fields of that structure:
F = fieldnames(S);
for k = 1:numel(F)
S.(F{k}).y_values.values
end
Note that if you have multiple .mat files then your code would also be much simpler if each .mat contained variables with exactly the same names. Having lots of numbered variables is a sign that you are doing something wrong (i.e. you should be using indexing).

5 件のコメント

John Doe
John Doe 2019 年 6 月 13 日
I have read that, for the sake of this question I didn't think it was relevant my code, I wrote the code specifically for this question. I still have the same question.
How do I generate the structure path in a for loop to export this multiple times?
For one structure I could do this:
data(1,:) = S.Signal_00.y_values.values
However I would like to access multiple signals from that structure using a for loop.
Stephen23
Stephen23 2019 年 6 月 13 日
編集済み: Stephen23 2019 年 6 月 13 日
"For one structure I could do this:"
Yes, that works fine... but that is NOT what I told you to do.
"However I would like to access multiple signals from that structure using a for loop. "
Did you read my answer and see the code where I showed you exactly how to do that?
Here is the code again, in case you missed it the first time:
F = fieldnames(S);
for k = 1:numel(F)
S.(F{k}).y_values.values
end
And here is the very informative link again too:
John Doe
John Doe 2019 年 6 月 13 日
編集済み: John Doe 2019 年 6 月 13 日
Edit made a typo. This works - apologies for confusion.
Thank you
Stephen23
Stephen23 2019 年 6 月 13 日
F is a cell array of fieldnames, you need to use idexing, e.g.
S.(F{k}).y_values.values
% ^^^ do not forget the index!
John Doe
John Doe 2019 年 6 月 13 日
Precisely what I forgot! Thank you

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2018a

質問済み:

2019 年 6 月 13 日

コメント済み:

2019 年 6 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by