How to tell a loop to wait for an iteration?
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nancy
le 20 Nov 2014
Réponse apportée : Image Analyst
le 29 Nov 2014
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 commentaires
Geoff Hayes
le 20 Nov 2014
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?
Réponse acceptée
Geoff Hayes
le 20 Nov 2014
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.
Plus de réponses (1)
Image Analyst
le 29 Nov 2014
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.
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!