How to check whether multiple variables are numeric using a loop

조회 수: 5 (최근 30일)
Richard Rees
Richard Rees 2021년 6월 7일
댓글: Richard Rees 2021년 6월 8일
Hi everyone,
My problem is how to check whether a variable within the workspace is numeric. I know about "isnumeric" the problem I have is trying to actually get MATLAB to tell me whether the variable content is numeric and not the string that I use within my loops to identify the variable within the workspace.
The attached code forms part of a conditional set of instruction on how to load in data, depending on whether it exists (non pre-allocated memory) or is an actual file.
Could you help?
Many thanks
D_mat_SA_search_master = ["D_mat_N_sqz_27.mat" "D_mat_G_sqz_27.mat"]
for aa = 1:size(D_mat_SA_search_master,2)
File_loc_ID = fullfile(Main_sim_directory,D_mat_SA_search_master(Main_loop,aa));
%If variable not in WS and is a file - load up
if ~isnumeric(contains(WS,D_mat_search(aa))) && isfile(File_loc_ID) % Problem here
isfile(File_loc_ID)
File_loc_ID = string(File_loc_ID);
importdata(File_loc_ID);
fprintf ('Loading - %s \n.',string(File_loc_ID));
continue
% Variable is present from loading - skip to next section
elseif isnumeric(contains(WS,D_mat_search(aa)))
continue
% skip this
% Variables need to be calculated and saved
else
% Do stuff
end
end
  댓글 수: 1
Matt J
Matt J 2021년 6월 7일
and not the string that I use within my loops to identify the variable within the workspace.
It is bad practice to be assigning names dynamically to data read in from files,

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2021년 6월 7일
varname=D_mat_search(aa);
if exist(varname,'var') && isnumeric(eval(varname)) && isfile(File_loc_ID) %BAD

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by