Saving the timestamp for each iteration in an array

조회 수: 15 (최근 30일)
Lola
Lola 2022년 11월 30일
댓글: Jon 2022년 12월 1일
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');

답변 (2개)

Jon
Jon 2022년 11월 30일
편집: Jon 2022년 11월 30일
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
Lola
Lola 2022년 11월 30일
편집: Lola 2022년 11월 30일
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 2022년 12월 1일
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
Alberto Cuadra Lara 2022년 11월 30일
편집: Alberto Cuadra Lara 2022년 11월 30일
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
Lola
Lola 2022년 11월 30일
편집: Lola 2022년 11월 30일
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 2022년 11월 30일
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

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

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by