Filter löschen
Filter löschen

Saving the timestamp for each iteration in an array

43 Ansichten (letzte 30 Tage)
Lola
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
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');

Antworten (2)

Jon
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
Lola
Lola am 30 Nov. 2022
Bearbeitet: Lola am 30 Nov. 2022
Greetings to you Jon. When trying to run the code provided in the command window. I get the error
Unable to perform assignment because of value type 'datetime' is not convertible to 'double'
Error in app.iwant_time(k)=date; Caused by Error using datetime/double
Undefined function for 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a date origin, then convert to numeric using SECONDS,MINUTES,HOURS,DAYS, or YEARS functions.
Also the xlswrite functions are underlined and the warning reads
This statement (and possibly following ones) cannot be reached.
Jon
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
pause(1);
fprintf('%10s | %10f\n', app.iwant_time{end}, app.iwant{end});
if length(app.iwant) > 5
return
end
end
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.data, app.filename, 'Sheet', app.sheet_name);
  5 Kommentare
Lola
Lola am 30 Nov. 2022
Bearbeitet: Lola am 30 Nov. 2022
Okay I was abit confused on the variable app.data because I don't see it anywhere else in the code besides xlswrite. Can you please explain me how the sensor values and time gets stored in that variable.
Lola
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.

Community Treasure Hunt

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

Start Hunting!

Translated by