而在Matlab中循环增加一个数

时间:2015-01-21 02:59:06

标签: matlab while-loop

我是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是从Emotive EEG设备每隔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;

1 个答案:

答案 0 :(得分:2)

这两行

var2 = 2;
var2 = var2+1

相同
var2 = 3;

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