How to effectively check a list of variables for existence and then check "isvector"

7 views (last 30 days)
I have ~10 input data to check,
First check if this input data exists, if it exists, check if it's a vector, if it doesn't exist triggers a warning that the RMSE can't be calculated.
Secondly, if the data is a vector, store the data, if no triggers an error. It's OK for data1 to be empty, but data1 needs to be N by 1 or 1 by N.
My current code is as follows, and I used structure format to avoid using "eval".
if exist('data1','var')
if isvector(data1)
checkdata.data1 = data1;
else
error('ERROR: Dimension of data1 needs to be N-by-1 or 1-by-N, please check your input.');
end
else
checkdata.data1 = [];
warning('User input for data1 is empty, data1 RMSE will not be calculated.');
end
However I need to perform this for 10 data. What's the best loop strategy if I don't repeat this block 10 times?
if exist('data1','var')
if isvector(data1)
checkdata.data1 = data1;
else
error('ERROR: Dimension of data1 needs to be N-by-1 or 1-by-N, please check your input.');
end
else
checkdata.data1 = [];
warning('User input for data1 is empty, data1 RMSE will not be calculated.');
end
if exist('data2','var')
if isvector(data2)
checkdata.data2 = data2;
else
error('ERROR: Dimension of data2 needs to be N-by-1 or 1-by-N, please check your input.');
end
else
checkdata.data2 = [];
warning('User input for data2 is empty, data2 RMSE will not be calculated.');
end
if exist('data3','var')
if isvector(data3)
checkdata.data3 = data3;
else
error('ERROR: Dimension of data3 needs to be N-by-1 or 1-by-N, please check your input.');
end
else
checkdata.data3 = [];
warning('User input for data3 is empty, data3 RMSE will not be calculated.');
end
% repeat for data1 to data10 ....
  3 Comments
Jack L
Jack L on 15 Oct 2020
Edited: Jack L on 15 Oct 2020
@Walter Roberson The data are loaded from "mat" files, some may exist but some may not. And the mat files are supplied to me by customers. They can't save the data in other formats.

Sign in to comment.

Answers (2)

per isakson
per isakson on 15 Oct 2020
Edited: per isakson on 15 Oct 2020
Maybe it's good enough to run
>> whos data*
Name Size Bytes Class Attributes
data1 0x0 0 double
data3 0x0 0 double

Stephen23
Stephen23 on 15 Oct 2020
Edited: Stephen23 on 15 Oct 2020
"The data are loaded from "mat" files, some may exist but some may not."
Once those .mat files are already loaded directly into the workspace then there is no way to do this efficiently (either in terms of magic variable names or copy-and-paste code). If you want to do this in an easy efficient effective way, then you need to load into an output variable:
S = load(...);
The output is a scalar structure containing one field for each variable in the .mat file, you can trivially call isfield on that. And of course, this loading and checking can be easily and efficiently performed in a loop:
Much much much better than numbering variable names or field names would be to use indexing. You should be using indexing. Note that you can easily refer to the specific field using dynamic fiednames:

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!