在Matlab中的While循环中递增数字 [英] While loop in Matlab to increment a number

查看:261
本文介绍了在Matlab中的While循环中递增数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

所以我想做的事情是在变量为true的情况下增加一个数字,在这种情况下,我将在for循环中使用该数字.让我给你举个例子.

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

所以var2应该给我从2递增的数字,直到while循环为false.

我已经尝试了很多,但是却无法获得.

有人可以帮助我吗?

提前谢谢.

更新

因此有两个按钮startstop,而handles.h.data是一个128x14矩阵,该矩阵每隔0.5秒从Emotive EEG设备生成一次,因此此变量out每0.5秒更新一次. /p>

因此,每当我按下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;走出循环 ?

I am new to Matlab and this should be a stupid question.

So the thing i wanted to do is to increment a number if a variable is true, in which i would be using that number to in the for loop. let me give you an example.

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

so var2 should give me incremented number from 2 till the while loop is false.

I have tried a lot but just not able to get it.

could any one help me?

thanks in advance.

Update

so there are two buttons, start and stop and handles.h.data is a 128x14 matrix generated every 0.5 seconds from an Emotive EEG device, so this variable out updates every 0.5 seconds.

So when ever i press on the stop button i want this loop to end. Is this correct?

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;

解决方案

These two lines

var2 = 2;
var2 = var2+1

are the same as

var2 = 3;

Perhaps you meant for var2 = 2; to go outside the loop?

这篇关于在Matlab中的While循环中递增数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆