Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

While loop in Matlab to increment a number

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...