How to tell a loop to wait for an iteration?

조회 수: 31 (최근 30일)
Nancy
Nancy 2014년 11월 20일
답변: Image Analyst 2014년 11월 29일
I am trying to read data being exported to excel from matlab in a moving window size of 5. for example I want to read rows 1:5, 2:6 etc. Ideally 10 rows of data will be transfered from matlab to excel and I want to get 6 samples from the ten rows. The loop below works fine if all the data is present before I run the loop. I am running into an issue now that if the data still didn't transfer over to excel I get empty answers for my num{cell} ( I get [] ). Is there anyway that I can tell excel to wait until the 5 first rows are added to start the loop's first iteration and then wait for an additional row to be added to do the next iteration?
for i=1:6
num{count}=xlsread('testdata.xlsm','Sheet2',strcat('A',num2str(i),':B',num2str(i + 4)));
count= count +1;
end
Also is there anyway I can take out the cells in num and have each cell be written as an array matrix?
Thank you so much!
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2014년 11월 20일
Nancy - is the data going from MATLAB to Excel, or Excel to MATLAB? You are using the xlsread command so that suggests the latter, but your question mentions a couple of times that you are exporting data to Excel (in which case you should be using xlswrite). Or do you have a separate process that writes data to Excel and another that will read back from it?
Nancy
Nancy 2014년 11월 20일
i will be opening separate matlab windows that are sending values being read from Arduino sensors.

댓글을 달려면 로그인하십시오.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 11월 20일
Nancy - I don't think that you can tell Excel to wait until it has enough rows. You may have to continually query for this data until the correct number of rows is returned. Something like
for k=1:6
atIter = 0;
maxIters = 100;
while atIter<maxIters
atIter = atIter + 1;
data = xlsread('testdata.xlsm','Sheet2',strcat('A',num2str(k),':B',num2str(k + 4)));
numRows = size(data,1);
if numRows==5
num{count} = data;
count = count + 1;
break;
end
% wait for 100 milliseconds
pause(0.1);
end
end
In the above we guard against getting stuck in a while loop by only allowing 100 iterations of this loop, where we query the Excel spreadsheet for data in the indicated fields. If we retrieve less than the expected number of rows then we pause for 100 milliseconds, and continue with the next query. If ever we retrieve the 5 rows, then we save them to your cell array and break out of the while loop so that we can continue with the next k.
Please note that the above is untested and gives you an idea of what you could do. There may be better options.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 11월 29일
If you are reading data from your arduino, you should not be using xlsread() or xlswrite(). You should be using ActiveX (if you're on Windows) since it will be very much more responsive and faster. Attached is an ActiveX demo for Excel.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by