While 在 Matlab 中循环递增一个数字

While loop in Matlab to increment a number

我是 Matlab 的新手,这应该是一个愚蠢的问题。

所以我想做的是在变量为真时增加一个数字,我将在 for 循环中使用该数字。让我举个例子。

global var1;
var1 = true;
while (var1)
   var2 = 2;
   var2 = var2+1
   for var3=1:var2
       do something...
   end
end

所以 var2 应该给我从 2while 循环是 false.

的递增数字

我已经试了很多了,就是做不到。

有人能帮帮我吗?

提前致谢。

更新

所以有两个按钮,startstophandles.h.data 是一个 128x14 矩阵,每 0.5 秒从情绪脑电图设备生成,所以这个变量 out 每 0.5 秒更新一次。

所以每当我按下 stop 按钮时,我都希望这个循环结束。这是正确的吗?

function start_Callback(hObject, eventdata, handles)
% hObject    handle to start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global true_false;
true_false = true;
a =1;
axes(handles.eeg_dynamic)
while (true_false)

    out = nan([size(handles.h.data),4]);
    for k = 1:a % a to be incremented by 1
        out(:,:,k) = handles.h.data + rand(1);
        plot(out(:,:,k));
        pause(0.5);
    end
    a=a+1;
end


% This converts the above out to 1D matrix and puts it in the workspace
axes(handles.eeg_final)
for eeg = 1:size(out(:,:,:),3)
        eeg_output_1d = permute(out,[1 3 2]);
        eeg_output_1d = reshape(eeg_output_1d,[],size(out,2),1);
        plot(eeg_output_1d);
        assignin('base','eeg_output_1d',eeg_output_1d)
    end

% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject    handle to stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global true_false;
true_false = false;

这两行

var2 = 2;
var2 = var2+1

相同
var2 = 3;

也许您的意思是 var2 = 2; 外面 循环?