Search code examples
matlabvariablesconstantscycleexecution

How to keep a constant value of a variable during the execution of the code in Matlab


I have a short (and I reckon silly as well) question about manipulation of variables in Matlab codes. How can I keep a constant value of a variable (assigned during the execution of the code), while the code executes further? So basically put the value into the memory and don't change it all. As an example of the code on which I am working right now:

 if SystemTriggered ==1;     
   if Accelerationflag == 1;
     for n = 1:1:100
         AOrder = 1/2*HMSpeed^2/(Acc+n*2*pi);
            if AOrder<Alim;
                k = n;
                Accelerationflag = 0;
                break;
            end
        end
    end
    Offset = k;
    AccOffset = PhaseIni - Offset*2*pi;
    %Derivation conditions
    if My condition here;
        HmSpeedReached = 1;
    end
  end

So I am looking for an option, how I can keep the calculated value of "Offset" when I got "HmSpeedReached =1". Since we have a "for" cycle in the beginning (that will assign a value to K and then to Offset), so I just need to keep that number as a value of the variable all the time, after the condition of HmSpeedReached is satisfied... Thank you in advance.


Solution

  • If I understand correctly then you want the following:

    • Keep assigning Offset = k whilst looping through your code
    • If HmSpeedReached = 1 has been set, then don't change the Offset value

    This code (adapted from yours) should work

    % Initially make sure HmSpeedReached is not equal to 1
    HmSpeedReached = 0;
    % Your code with addition...
    if SystemTriggered ==1;     
       if Accelerationflag == 1;
           for n = 1:1:100
               AOrder = 1/2*HMSpeed^2/(Acc+n*2*pi);
               if AOrder<Alim;
                   k = n;
                   Accelerationflag = 0;
                   break;
               end
           end
        end
        % Add a condition here to check if Offset should be updated, if HmSpeedReached is not 1
        if HmSpeedReached ~= 1
            Offset = k;
        end 
    
        AccOffset = PhaseIni - Offset*2*pi;
        %Derivation conditions
        if My condition here;
          HmSpeedReached = 1;
        end
    end
    

    If instead you want to save a vector of Offset values, every time some condition is met, then use something like this:

    Offset = [];
    HmSpeedReached = 0;
    if SystemTriggered ==1;     
        if Accelerationflag == 1;
            for n = 1:1:100
                AOrder = 1/2*HMSpeed^2/(Acc+n*2*pi);
                if AOrder<Alim;
                    k = n;
                    Accelerationflag = 0;
                    break;
                end
            end
        end
        % Add a condition here to check if Offset should be updated, if HmSpeedReached is not 1
        if CONDITION FOR SAVING OFFSET
            Offset = [Offset, k];
        end 
    
        AccOffset = PhaseIni - Offset*2*pi;
        %Derivation conditions
        if My condition here;
            HmSpeedReached = 1;
        end
    end