If a structure exists in a file

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by