How to filter out missing variables for functions when importing .txt files

1 view (last 30 days)
I am importing a text file and evaluating the data in the file versus baseline data. The problem I am having is that the text file to be evaluated is sometimes missing variables.
How can I efficiently besides, using ~exist() for every variable, filter out the data that is not present

Accepted Answer

Sean de Wolski
Sean de Wolski on 29 Mar 2013
Edited: Sean de Wolski on 29 Mar 2013
First, I warn you that this approach of "poofing" variables might be a bad idea that could lead to difficult to debug issues further down the line.
Second, to do this efficiently you could use the structure output from whos() and ismember() to identify missing variables:
clear;
x = 1;
y = 7;
varsWanted = {'x','y','z'};
varsPresent = whos;
missing = varsWanted(~ismember(varsWanted,{varsPresent.name}));

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!