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

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 comentarios

Are you using 'datepicker' to ask user to select the dates or an edit field?
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.

Iniciar sesión para comentar.

 Respuesta aceptada

  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 comentarios

I have yet again run into another problem. With my App, once the user imported a file, then selected Beginning and Ending dates, a uitable would pop up with all the information. However, in order for my to use 'timerange' I had to convert my table into a timetable using 'table2timetable' but once I've done that I can't seem to create a uitable. Here is my code for my uitable pop up thing and I'm also showing the error message I'm getting.
% Extract desired time period, if not all
TimeTableOut = table2timetable(InTable);
if ~isempty(app.BeginningEditField.Value)
%app.BeginningEditField.Value = datestr(app.BeginningEditField.Value);
%app.EndingEditField.Value = datestr(app.EndingEditField.Value);
StartValue = datetime(app.BeginningEditField.Value,'InputFormat',app.OutputDateTimeStringFormat);
EndValue = datetime(app.EndingEditField.Value,'InputFormat',app.OutputDateTimeStringFormat);
TR = timerange(StartValue, EndValue, 'closed');
TimeTableOut = TimeTableOut(TR,:);
end
%% Reading in the table as a uitable =================================================
% my_table = timetable2table(TimeTableOut);
fig = uifigure;
uit = uitable(fig,'Data',TimeTableOut);
uit.Position = [5 0 515 410];
% ===================================================================================
Here Are the Errors I'm getting
Error using uitable
Data must be a numeric, logical, string, cell, or table array
Error in uitable (line 53)
thandle = builtin('uitable', varargin{:});
Error using datetime (line 640)
Unable to convert '2012/05/16 10:00:00' to datetime using the format 'yyyy-MM-dd HH:mm:ss'.
Again, you have to check what data is supported in uitable(). Might it be that TimeTable is not supported by "data" in uitable()? Try it separately in command line and figure out the proper conversion.
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.
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 2 de Jun. de 2020

Comentada:

el 2 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by