フィルターのクリア

If a structure exists in a file

5 ビュー (過去 30 日間)
Tala
Tala 2021 年 4 月 17 日
回答済み: Bjorn Gustavsson 2021 年 4 月 17 日
I load several *.MAT files using a for loop, store the structures that I need, and get rid of the file. Each file contains several structures and I am only intersted in some of them. The code I use is below:
for i=[120:131 133:140 144:153]
i
FileName=strcat(['DATA_' num2str(i) '.mat']);
load(FileName)
Bot{i}=(((cDA_Bottom_Speed.DATA.*1000)-4)/0.2133.*3.6/1.608)';
Mid{i}=(((cDA_Middle_Speed.DATA.*1000)-4)/0.2133.*3.6/1.608)';
Top{i}=(((cDA_Top_Speed.DATA.*1000)-4)/0.2133.*3.6/1.608)';
Ex{i}=(((cDA_External_Speed.DATA.*1000)-4)/0.2133.*3.6/1.608)';
end
In some files, the struture "cDA_External_Speed" doesnt exist. How can I check if it exists, I assign that to a cell ("Ex{i})". I am thinking a condition to use above
Ex{i}=(((cDA_External_Speed.DATA.*1000)-4)/0.2133.*3.6/1.608)';
The image shows the variables I have after importaning the file.
Thanks for your help

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2021 年 4 月 17 日
You can explicitly check for the existense of a variable in a .mat-file:
qwe = whos('-file','your-filename.mat','cDA_External_Speed')
if that variable doesn't exist whos will return an empty array, otherwise you'll get a struct with contenst something like this:
qwe =
struct with fields:
name: 'Tr'
size: [125 1]
bytes: 1000
class: 'double'
global: 0
sparse: 0
complex: 0
nesting: [1×1 struct]
persistent: 0
HTH

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by