How to extract values from each structure in the .mat file

37 ビュー (過去 30 日間)
Sanket Nikam
Sanket Nikam 2020 年 10 月 14 日
コメント済み: Sanket Nikam 2020 年 10 月 19 日
Hello,
I have a .mat file that contains multiple 1*1 Structures with various names. I want to extract values of signals and assign it to a variable with same name as that Structure. And I want to repeat this process for all Structures present in my .mat file. How do I write a script to automate this process?
Below is an example of this process I would like to automate.
  1. My .mat file contains structure with signal name X (1*1 struct) that contains various fields under it
  2. I would like to extract values located at field - X.signals.values where values is 1792*1 double column with actual data values recorded for signal X
  3. I would like to assign signal name X to extracted values and save it to workspace with same signal name X
  4. I would like to repeat steps 1-3 for all structure signals in my .mat file
Please help me understand how to automate above process with MATLAB script. Please let me know if you need more explanation of above process.
  2 件のコメント
Sanket Nikam
Sanket Nikam 2020 年 10 月 19 日
Hello Walter,
Thanks for sharing this link! Certainly it is worth reading. Appreciate your help!

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

採用された回答

Sindar
Sindar 2020 年 10 月 15 日
keeps the names, but as fieldnames in a new struct, signals, rather than variables (see Walter's comment):
data = load('data.mat');
data_fields = fieldnames(data);
for ind=1:length(data_fields)
X = data_fields{ind};
signals.(X) = data.(X).signals.values;
end
(if you have tons of fields, there may be a faster way, likely leveraging tables)
  1 件のコメント
Sanket Nikam
Sanket Nikam 2020 年 10 月 19 日
Hello Sindar,
Thanks for your inputs with sample code. I was able to make it work with your suggested approach. I just added 'assignin' line to be able to extract signals to workspace for easier post-processing. Please see below final code that is working fine for my data analysis purpose. Again, Thank you so much for your help!
data = load('201007_TestCase-1.mat');
data_fields = fieldnames(data);
for ind=2:length(data_fields)
X = data_fields{ind};
%signals.(X) = data.(X).signals.values;
assignin('base',string(X),data.(X).signals.values);
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

タグ

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by