Creating a time input dialog and plotting in the time period.

조회 수: 10 (최근 30일)
Ramo Rafsel
Ramo Rafsel 2020년 11월 2일
편집: Ramo Rafsel 2020년 11월 2일
I would like to plot specific variables with their time and values, that have been saved and imported from Excel. Which I already did.
Now I have to add a new part in the code, which is adding an input dialog where I can write a time stamp or period which the variable would only be plotted between the start time (st) and the end time (et) in the "HH:MM:SS" format that have been written in the dialog. I tried the code below and I got the following error for the two last disp lines of the inputbox.
Error using horzcat
The following error occurred converting from char to struct:
Conversion to struct from char is not possible.
Is there a way how write the two start and end times and plot the variables ( which are written in the U variable) only in the time stamp that was put in the dialog box?
answer = questdlg('is there a time stamp available? y = yes, n = no',...
'input',...
'y','n','n');
if (answer == 'y')
st.Format='hh:mm:ss';
et.Format='hh:mm:ss';
st = input( ' Please add the starttime in [HH:MM:SS] '])
et = input( ' Please add the endtime in [HH:MM:SS] '])
end
S = load('matlabtable.mat');
T = S.HV801000front10Vml2;
U = {'E1_ABD_FoamEventCnt','E1_LD_ActualStatus','H1_PRESSURE_S15hPa'};
for k = 1:numel(U)
X = T.Variable==U(k);
Wert = T.Value(X);
Uhrzeit = round(seconds(T.time(X)/1000));
Uhrzeit.Format='hh:mm:ss';
Uhrzeit = sort( Uhrzeit,'ascend');
nexttile
plot(T.time(X),T.Value(X))
xlabel('time')
title(char(U(k)),'Interpreter','none')
end

채택된 답변

VBBV
VBBV 2020년 11월 2일
편집: VBBV 2020년 11월 2일
You have additional ] at the end of input box. Plus you have defined st and at as structure. Define them outside the if condition.
% if true
% code
%end
answer = questdlg('is there a time stamp available? y = yes, n = no',...
'input',...
'y','n','n');
if (answer == 'y')
% st.Format='hh:mm:ss'; % place this after input box
% et.Format='hh:mm:ss';
st = input( ' Please add the starttime in [HH:MM:SS] ') % here
et = input( ' Please add the endtime in [HH:MM:SS] ')% here
end
  댓글 수: 3
VBBV
VBBV 2020년 11월 2일
% if true
% code
% end
plot(T.time(X),T.Value(X));
xlim([st et])
Try it
Ramo Rafsel
Ramo Rafsel 2020년 11월 2일
It doesn't work, I get the following errors:
Error using xlim (line 31)
When specifying numeric values, enclose the input arguments in parentheses.
Error in Messdiagrammcode (line 20)
xlim([st et]);
since I use it twice for two loops I get a second error:
Error using xlim (line 31)
Specify limits or one of the 'mode', 'auto', or 'manual' options.

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

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2020년 11월 2일
hello
I think there is some input dialog window missing for the start and end time inputs
I suggest the following code :
answer = questdlg('is there a time stamp available? y = yes, n = no',...
'input',...
'y','n','n');
if (answer == 'y')
prompt = {'Enter start Time indexes [HH:MM:SS]:','Enter stop Time indexes [HH:MM:SS]:'};
dlgtitle = 'Time indexes HH:MM:SS ';
dims = [1 35];
definput = {'13:20:00','13:50:00'};
time_answer = inputdlg(prompt,dlgtitle,dims,definput);
st = time_answer{1,:}; % cell of char array
et = time_answer{2,:}; % cell of char array
% st.Format='hh:mm:ss';
% et.Format='hh:mm:ss';
% disp([st ' Please add the starttime in [HH:MM:SS] '])
% disp([et ' Please add the starttime in [HH:MM:SS] '])
end
% conversion time in seconds for test in for loop below
[Y, M, D, H, MN, S] = datevec(st);
st_secs = S+MN*60+H*3600;
[Y, M, D, H, MN, S] = datevec(et);
et_secs = S+MN*60+H*3600;
S = load('matlabtable.mat');
T = S.HV801000front10Vml2;
U = {'E1_ABD_FoamEventCnt','E1_LD_ActualStatus','H1_PRESSURE_S15hPa'};
for k = 1:numel(U)
X = T.Variable==U(k);
Wert = T.Value(X);
time = (T.time(X)/1000);
% reduce plot to time index between start and stop
ind = find(time>= st_secs & time<= et_secs);
time = time(ind);
Wert = Wert(ind);
Uhrzeit = round(seconds(time));
Uhrzeit.Format='hh:mm:ss';
Uhrzeit = sort( Uhrzeit,'ascend');
nexttile
plot(Uhrzeit,Wert);
xlabel('time')
title(char(U(k)),'Interpreter','none')
end
  댓글 수: 1
Ramo Rafsel
Ramo Rafsel 2020년 11월 2일
편집: Ramo Rafsel 2020년 11월 2일
I thought about adding another input box for the start and end time but since I could write them in the command window I havent added the box in the code, but I guess it is better, so thanks a lot for the help!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by