Anyway to determine if a variable exists in a mat file (in app designer environment)?

33 vues (au cours des 30 derniers jours)
Kris Hoffman
Kris Hoffman le 10 Juil 2018
Commenté : Paulo Fonte le 9 Jan 2022
Pulling hair out trying to find a way to check for the existence of a variable in a 200+MB mat file. I obviously don't want to load into every function and I'm trying to stear clear of the assignin and evalin commands. So far I've tried..
[app.fileName,PathName] = uigetfile('*.mat','MultiSelect','off');
app.fileName = erase(app.fileName,".mat");
cd(PathName);
app.fileName;
m = matfile(app.fileName)
This gives me a 1x1 "matlab.io.MatFile" type in the workspace. And when I try to see if a certain variable exists using "exist" or "isfield", I get logical 0. I don't know if there is a way to convert the matfile contents into a list of some sort or if there's a simpler way to check for variables. I can pull the data out of the variable with no problem but when I simply ask if it exists..
if isfield(m,'SomeVariable')
SomeVariable = m.SomeVariable;
end
or
if exist('m.SomeVariable','var')
SomeVariable = m.SomeVariable;
end
  3 commentaires
Steven Lord
Steven Lord le 10 Juil 2018
The census.mat file contains two variables, cdate and pop.
x = matfile('census.mat');
Does 'cdate' exist in census.mat?
varIsInMat = @(name) ~isempty(who(x, name));
checkcdate = varIsInMat('cdate')
Does 'doesnotexist' exist in census.mat? [I guess I gave that one away.]
checkDNE = varIsInMat('doesnotexist')
Paulo Fonte
Paulo Fonte le 9 Jan 2022
OK. who() works for matfile objects.
But it is rather strange that getfield() works even in nested structs (e,g. getfield(MatfileObj,'Geo','Name'), where . indexing doesn't, but isfield() has not the same behaviour.

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 10 Juil 2018
Use the who or whos methods of your matlab.io.MatFile object.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by