Error using matlab.ui.​control.Ed​itField/se​t.Value 'Value' must be a character vector or a string scalar.

조회 수: 9 (최근 30일)
So I've using App Designer to import a file. I'm giving the user an option to pick Beginning and Endings dates for the file that they are importing. Here is some of my code that I've used in the past (as a function) to check to see if the user put anything for the Beginning dates and if they did, it uses 'datetime' and 'timerange' to and the dates to the readtable function.
if ~isempty(app.BeginningEditField.Value)
app.BeginningEditField.Value = datetime(app.BeginningEditField.Value,'InputFormat',app.OutputDateTimeStringFormat);
app.EndingEditField.Value = datetime(app.EndingEditField.Value,'InputFormat',app.OutputDateTimeStringFormat);
TR = timerange(app.BeginningEditField.Value, app.EndingEditField.Value, 'closed');
InTable = InTable(TR,:);
end
The variable app.OutputDateTimeStringFormat is set to 'yyyy/MM/dd HH:mm:ss'
Whever I type in a date in the Beginning and Ending Edit Field (with the same format as the app.OutputDateTimeStringFormat) I get this error.
Error using matlab.ui.control.EditField/set.Value (line 98)
'Value' must be a character vector or a string scalar.
I've tried using 'num2str(app.BeginningEditField.Value)' to possible change that into a string which could be used with this function but that does not work.
P.S. this error occurs on the 2nd and 3rd line. (where it uses 'datetime' function)
  댓글 수: 2
Sampath Rachumallu
Sampath Rachumallu 2020년 6월 2일
Are you using 'datepicker' to ask user to select the dates or an edit field?
Forrest Ward
Forrest Ward 2020년 6월 2일
I am using an Edit Field because I needed Hours, Minutes, and Seconds which I don't think the date picker has those options, but I could be wrong.

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 6월 2일
편집: Fangjun Jiang 2020년 6월 2일
  1. The value for "app.BeginningEditField.Value" needs to be a character vector or string scalr. datetime() returns an object of the "datetime" class. That is the mismatch.
  2. Try datestr() instead of num2str()
  3. You are re-assign "app.BeginningEditField.Value" in the code. Would using a separate variable avoid this problem?
if ~isempty(app.BeginningEditField.Value)
StartValue = datetime(app.BeginningEditField.Value,'InputFormat',app.OutputDateTimeStringFormat);
EndValue = datetime(app.EndingEditField.Value,'InputFormat',app.OutputDateTimeStringFormat);
TR = timerange(StartValue, EndValue, 'closed');
InTable = InTable(TR,:);
end
  댓글 수: 4
Forrest Ward
Forrest Ward 2020년 6월 2일
So like the error says, the data supported by uitable() needs to be string, cell, table array, etc. So in order to convert my time table to that format I tried using 'timetable2table(TimeTableOut)' which works in the command line but does not work in the App Designer for some reason.
Forrest Ward
Forrest Ward 2020년 6월 2일
Hold up, using 'timetable2table' actually worked!! And I found out my problem with the dates! My input for the dates just needed to be in a different format.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by