If a structure exists in a file

6 views (last 30 days)
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

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 17 Apr 2021
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

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!