eval function in for-loop and change of numbers bigger through NaN
    8 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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 Commenti
  Steven Lord
    
      
 il 10 Giu 2024
				A = magic(6)
[minValue, minLocation] = min(A, [], 1)
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
Risposta accettata
  Matlab Pro
      
 il 10 Giu 2024
        Hi @Franziska
I simply combine your inputs to a simple code (with indexes 1-->4):
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 Commenti
  Stephen23
      
      
 il 10 Giu 2024
				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.
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Matrix Indexing in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




