MATLAB: How to process multiple matrices. Checking whether the elements of a matrix are within the specified range

for loopfor loop multiple matricesmultiple matricesmultiple variablesprocess for loopprocess matrices

Hello. I have 55 matrices 288×1 named Load1, Load2, Load3, …, Load55. I need to create a for loop which checks whether the values are between 207 and 253. If one of the values exceeds 253 or is less than 207, I need to display the name of this variable. For example, one of the elements of matrix Load7 is 205. In this case, the program should display 'Load7'. Ultimately, I need to get a list of variables which values are not within specified range. Also, the script has to calculate the total number of variables which values are not within the range.
The output of the script shall look like, for example:
The Loads which are not within the range: Load7, Load25, Load37. The total number of loads: 3
Any help is greatly appreciated. Thank you.

Best Answer

Given you heed the advice in the comment so the data are in array loads
lo=207; hi=253; % set the limits desired
ix=iswithin(loads,lo,hi); % check all are ok, ix is logical array of locations
isOK=all(ix); % if T, all are ok for each column
if all(isOK)
fprintf('All is well\n')
else
bad=sum(~isOK); % number failed columns (loads)
fmt=['Load %2d' repmat(', %2d',1,bad-1) '\n'] % format string to print offending load(s)
fprintf(fmt,find(~isOK)) % and print the message
fprintf('Total number of loads failed: %d\n',bad) % and the total
end
CAUTION: Air code, untested, but should be close.
iswithin is a utility function of mine--
>> type iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>
ADDENDUM
Dawned on me not that hard to build some data to test...
>> loads=round(randn(20,3)*12+230)
oads =
234 237 224
208 229 226
242 238 217
259 233 224
242 236 228
226 212 231
235 218 229
218 225 237
253 231 231
241 244 252
239 227 234
219 245 252
234 236 221
223 244 236
226 232 227
223 222 237
218 212 237
219 232 204
227 240 214
210 226 213
>> ix=iswithin(loads,lo,hi);
>> isOK=all(ix)
isOK =
0 1 0
>> bad=sum(~isOK);
>> fmt=['Load %2d' repmat(', %2d',1,bad-1) '\n'];
>> fprintf(fmt,find(isOK==false))
Load 1, 3
>> fprintf('Total number of loads failed: %d\n',bad)
Total number of loads failed: 2
>>
Looks OK to me...