필터 지우기
필터 지우기

How I write an exist inside a for loop that will continue if the variable does not exist?

조회 수: 20 (최근 30일)
Hello,
I'm running an experiment where people perform a short task, they are recorded via interial measurement units, and at about halfway I click one of the markers that logs an event, as the broadly halfway point. Using this, I seperate total time, as well as the first and second time. However, the problem is that sometimes I don't click or make the marker, for whatever reason. I'm using a MATLAB for loop to calculate this part, however if the event marker does not exist the loop throws an error. I'm hoping to make it not throw an error and just calculate the total completion time then, not the first and second half. The problem seems to primarily come from the exist function, which I'm sure I'm not using properly. I should mention that the data is coming from h5 files as well. Additionally, n_trials is a variable that changes for every subject, it simply reflects how many of the output files there are, based on how many times they completed the course.
The bolded portion is where everythign appears to be going wrong, any advice would be greatly appreciated! Specifically, I'm not certain if the variable in this case can be used, or if I need to read it in somewhere else...
Code:
loop_count = 0;
for k = 1:n_Trials
loop_count = loop_count + 1;
disp(loop_count)
wrist{k} = h5read(Trials(k).name, '/Annotations/')
if exist(wrist{k}, 'var') == 0
continue
disp(wrist{k}.Time); %%% This is the Unix Epoch time at which they stepped on the foam
d2 = uint64(wrist{k}.Time);
d3 = datetime(d2,'ConvertFrom','epochtime','TicksPerSecond',1e6,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS');
% Now, Foot on Foam is D3, in date format
%10134/Time is going to be a vector of the eopch times from start to
%finish
wrist_time{k} = h5read(Trials(k).name, '/Sensors/10134/Time/');
e = wrist_time{k}(1,1);
e2 = uint64(e);
e3 = datetime(e2,'ConvertFrom','epochtime','TicksPerSecond',1e6,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS');
f = wrist_time{k}(end,1);
f2 = uint64(f);
f3 = datetime(f2,'ConvertFrom','epochtime','TicksPerSecond',1e6,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS');
time_diff = [e3, d3, f3];
% First number is first half completion time, second number is second
% half completion
calc = diff(time_diff);
calc.Format = calc.Format + ".SSSSSS";
first_half_dur = calc(1,1);
first_half = seconds(first_half_dur);
second_half_dur = calc(1,2);
second_half = seconds(second_half_dur);
total_completion_time = first_half + second_half - time_offset;
disp(total_completion_time)
% .Format gives me to the micro second option.
end
end
  댓글 수: 1
Steven Lord
Steven Lord 2023년 6월 22일
If your data is time-based (which it seems like it is) and you're using release R2023a or later, you may be interested in using an eventtable to record and work with the events in your data.

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2023년 6월 22일
편집: Fangjun Jiang 2023년 6월 22일
exist('wrist', 'var'), not exist(wrist, 'var')
exist('wrist', 'var'), not exist('wrist{k}', 'var')
I suggest using try-catch
try
wrist{k} = h5read(Trials(k).name, '/Annotations/');
catch
continue;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by