eval function in for-loop and change of numbers bigger through NaN

I have 108 variables with same name but with consecutive numbering. Each of this variables I want to check if there is a value bigger 1000. When yes, then it will be exchanged through NaN.
I try to solve with eval and for-loop, but it doesn't work.
I know the function which changes a value bigger 1000 through NaN.
Function: Temperatur_1(Temperatur_1>1000)=NaN;
Dynamics variabels are: Temperatur_1, Temperatur_2, etc.
eval-function: eval(strcat('Temperatur_',num2str(e)))
for i=1:108
tbd
end
What is the function for checking each variable for > 1000 and saving it in the same variable?
Thank you in advance for your support and solution.

5 Comments

"I have 108 variables with same name but with consecutive numbering."
Why?
Why is the data designed in such a way so that processing it will be slow, complex, and inefficient?
"Thank you in advance for your support and solution."
Use arrays. Using arrays is what makes processing data easy. Using arrays is how MATLAB works.
So far you did not tell us the most important information: how did you get all of those variables into the workspace?
These arrays are measurements from CAN which contents battery datas. These dats are all cell temperatures and voltages. Software is begin of development. That is the reason why some values in the arrays have SNA values. These values I want to changed for good plot and following calculation.
I have all these arrays in one matrix and that changed is working. But I need the single arrays for plotting.
But I need the single arrays for plotting.
Instead of creating a large number of individual variables, why not simply index into that matrix? This avoids creating a large number of variables in your workspace, which IMO makes it much more difficult to locate the information you're interested in.
A = magic(5)
A = 5x5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot(A(:, 4), 'o-') % Plot the 4th column; no need to create a variable A4
I need the single arrays because I'm looking for the minimum and maximum temperature value of each single temperature position. In a matrix is that for my small programming experience not possible. And I have all these arrays in one matrix for another topic.
A = magic(6)
A = 6x6
35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[minValue, minLocation] = min(A, [], 1)
minValue = 1x6
3 1 2 12 10 11
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
minLocation = 1x6
2 1 3 5 4 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
In column 4, based on the fourth elements of minValue and minLocation the minimum value of A is 12 and that occurs in row 5.
No need to create A1, A2, A3, A4, A5, and/or A6.
for n = 1:width(A)
fprintf("In column %d of A, the minimum value is %d" + ...
" and this occurs at (%d, %d).\n", n, minValue(n), minLocation(n), n)
end
In column 1 of A, the minimum value is 3 and this occurs at (2, 1). In column 2 of A, the minimum value is 1 and this occurs at (1, 2). In column 3 of A, the minimum value is 2 and this occurs at (3, 3). In column 4 of A, the minimum value is 12 and this occurs at (5, 4). In column 5 of A, the minimum value is 10 and this occurs at (4, 5). In column 6 of A, the minimum value is 11 and this occurs at (6, 6).

Sign in to comment.

 Accepted Answer

I simply combine your inputs to a simple code (with indexes 1-->4):
Please re-conider @Stephen23 comments
Temperatur_1 = 500;
Temperatur_2 = 300;
Temperatur_3 = 1001;
Temperatur_4 = 3;
suffixes = num2cell(1:4);
for i =1:4
vName = sprintf('Temperatur_%d',i);
eval(sprintf('%s(%s>1000) = NaN',vName,vName))
end

2 Comments

Perfect. It works. Big thank you!!
You should read this:
and also what the MATLAB documentation states about your data design: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array. If the data sets are of different types or sizes, use a structure or cell array."
Better data design would make your code simpler and more efficient.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2024a

Asked:

FD
on 9 Jun 2024

Commented:

on 10 Jun 2024

Community Treasure Hunt

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

Start Hunting!