Saving the timestamp for each iteration in an array

25 Ansichten (letzte 30 Tage)
Lola am 30 Nov. 2022
Kommentiert: Jon am 1 Dez. 2022
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
While true
% loop code"now","Format","HH:mm:ss");
app.iwant_time{k} = app.date_final;
%save button

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
while true && k < kMax
% loop code
app.iwant_time(k) = date;
% 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
  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.

Melden Sie sich an, um zu kommentieren.

Alberto Cuadra Lara
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
app = struct with fields:
test: {{1×1 cell}}
% Test 2
test = {1};
app.test(1) = test
app = struct with fields:
test: {[1]}
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
Time | Value
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
fprintf('%10s | %10f\n', app.iwant_time{end}, app.iwant{end});
if length(app.iwant) > 5
13:57:23 | 0.719906 13:57:24 | 0.098549 13:57:25 | 0.204576 13:57:26 | 0.052471 13:57:27 | 0.674501 13:57:28 | 0.490361
% Export data
writecell(, app.filename, 'Sheet', app.sheet_name);
  5 Kommentare
Lola am 30 Nov. 2022
I wanted the dates to display in one column on the excel sheet. Example. Date1 in columnA row 1 Date2 in columnA row 2 etc. But it displays in every cell of the first row in the excel sheet

Melden Sie sich an, um zu kommentieren.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by