Saving the timestamp for each iteration in an array
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a while loop running in appdesigner that is storing data from a sensor. For each updated value the sensor gives I want to store the time that this update occurred. I have managed to store the updated sensor values in an array and save it to an excel file. Although the column that i want to store the time is blank. Can I please request help on the code for storing the time stamp for each iteration. Below is the code I have tried
%start button
K=0
While true
% loop code
app.date=datetime("now","Format","HH:mm:ss");
app.date_final=cellstr(app.date)
K=K+1
app.iwant{k}=app.sensorvalue;
app.iwant_time{k} = app.date_final;
End
%save button
xlswrite('test.xls',app.iwant,'Visits','B1');
xlswrite('test.xls',app.iwant_time,'Visits','B1');
0 Kommentare
Antworten (2)
Jon
am 30 Nov. 2022
Bearbeitet: Jon
am 30 Nov. 2022
You had numerous errors in your code. A key one is that you were writing the sensor data and time to the same column (B1). Since the time values were written after the sensor data this would overwrite the sensor data and only display time. You however had other problems so that you were writing anything at all, and so this overwrite problem didn't show up. Here is a cleaned up version of your idea that should be closer to what you want.
Note in particular that you don't need to be putting everything into cell arrays. Also since you are writing columns in Excel you should be careful to have the data and times in n by 1 arrays, with your code they were in 1 by n arrays. Also you need to be careful about being consistent with capitalization, you used both k and K for your counter, you have to pick one or the other and stay with it, MATLAB is case sensitive so k and K are assumed to be different variables.
% set some parameters just for testing/debugging
kMax = 100;
sensorvalue = randn(kMax,1);
% preallocate arrays
app.iwant = zeros(kMax,1)
app.iwant_time = NaT(kMax,1)
%start button
k=0
while true && k < kMax
% loop code
date=datetime("now","Format","HH:mm:ss");
k=k+1
app.iwant(k)=sensorvalue(k);
app.iwant_time(k) = date;
end
% convert datetime array to Excel datenumber
app.iwant_time = exceltime(app.iwant_time);
%save button, time goes in column A, values go in column B
xlswrite('test.xls',app.iwant,'Visits','B1');
xlswrite('test.xls',app.iwant_time,'Visits','A1');
2 Kommentare
Jon
am 1 Dez. 2022
Hi Lola,
Sorry you are having difficulties. I just checked and the script I provided above runs without errors on my machine. Did you try just copying and pasting the entire code listing I have above into a script (.m file) and running it?
The errors you show would occur if perhaps you did not preallocate app.iwant_time(k) to an array of NaT (not a times ) as I did.
You must preallocate an array of datetimes to hold the dates using
app.iwant_time = NaT(kMax,1)
If you preallocated them to an array of doubles e.g.
app.iwant_time(k) = zeros(kMax,1)
then you get the errors you describe.
Alberto Cuadra Lara
am 30 Nov. 2022
Bearbeitet: Alberto Cuadra Lara
am 30 Nov. 2022
Hi Lola,
The property date_final is already a cell, so you are saving a cell value of the cell, which is not what you want.
% Test 1
test = {1};
app.test{1} = test
% Test 2
test = {1};
app.test(1) = test
Also, I would rather use writecell to not be restricted to Excel. Your code can be simplified as follows
% Define properties
app.iwant = [];
app.iwant_time = [];
app.filename = 'test.xls';
app.sheet_name = 'Visits';
% Loop
fprintf(' Time | Value\n'); % Debug - REMOVE
while true
% Get current time
app.iwant{end + 1} = rand(1); % Debug - change to -> app.sensorvalue
app.iwant_time{end + 1} = datetime("now", "Format", "HH:mm:ss");
% Debug - REMOVE
pause(1);
fprintf('%10s | %10f\n', app.iwant_time{end}, app.iwant{end});
if length(app.iwant) > 5
return
end
end
% Export data
writecell(app.data, app.filename, 'Sheet', app.sheet_name);
5 Kommentare
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!