If a structure exists in a file
조회 수: 5 (최근 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/586826/image.jpeg)
Thanks for your help
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!