AppDesigner Converting Table Data to Categorical Supported?

6 visualizaciones (últimos 30 días)
I am importing data from a .csv file and need to convert one of the columns to categorical variables. Conversion of Table data to categorical variables using the below code does not work in appdesigner
Data.IncludedInDistribution_=categorical(Data.IncludedInDistribution_);
The returned error is:
Struct contents reference from a non-struct array object.
Is there a work around?

Respuesta aceptada

David Ding
David Ding el 19 de Oct. de 2017
Hi Josh,
Thank you so much for sending the app! I have taken a look at it and now I know why you are getting the error. The reason is from the code on line 377:
NTA_SubScript(app, Histo_Plot, Int_Plot, data, 0)
Now, the input argument "data" is expected to be a struct created from "readtable" of the CSV file selected. I verified that "readtable" works on the CSV file given. However, the "data" variable that is getting populated is a local variable of the "Run1ButtonPushed" callback. Once you are inside the "RunButtonPushed" callback, the "data" variable is a new local variable created, with default value being empty, and hence if you place a breakpoint on this line, and examine the value of "data", you get:
data = []
Therefore, when you are trying to access
data.IncludedInDistribution_
You get the error:
Struct contents reference from a non-struct array object.
As expected, because you are trying to get a struct content, "IncludedInDistribution_" from a supposedly struct "Data". However, "Data" is null.
In order to fix this issue, a workaround is to assign a property, such as "Data" in the perperties block of the app, and access the data as "app.Data". That way, the variable "app.Data" will be globally accessed by all callback functions.
Thanks,
David
  1 comentario
Joshua Welsh
Joshua Welsh el 20 de Oct. de 2017
Thanks very much for your time and help David, I should have noticed that!

Iniciar sesión para comentar.

Más respuestas (2)

David Ding
David Ding el 18 de Oct. de 2017
Editada: David Ding el 19 de Oct. de 2017
Hi Josh,
I see that you are importing data from CSV file and trying to create a categorical array from it. Without knowing the details about the import itself, I will try to comment on the root cause of the error and possible workaround.
The error you are receiving is because the input argument, "Data.IncludedInDistribution_" is not one of the acceptable data types for the "categorical" function:
For example, if "Data.IncludedInDistribution_" is a table, which is not one of the acceptable data types, then you may use "table2cell" function to convert it into a cell array (which is one of the acceptable data types) first prior to calling "categorical".
Hope this helps.
Cheers,
David

Peter Perkins
Peter Perkins el 19 de Oct. de 2017
Without meaning to contradict David, a cell array is only one of many different inputs that the categorical function will accept. The input is NOT required to be a cell array. But it's impossible to say what's happening without knowing what you are passing in.
So, what type is Data.IncludedInDistribution_?
  4 comentarios
David Ding
David Ding el 19 de Oct. de 2017
Thanks Josh! Do you mind sharing your code for the part where you import the CSV file and up to the line:
Data.IncludedInDistribution_=categorical(Data.IncludedInDistribution_);
Where the error occurred?
Sincerely,
David
Joshua Welsh
Joshua Welsh el 19 de Oct. de 2017
Editada: Joshua Welsh el 19 de Oct. de 2017
Hi David,
Thanks very much for your help.
I've included a link to the app (I am unable to upload it here as .mlapp appears to be an unsupported file type) which should include all of the scripts - its not finished yet as I've encountered this problem - so its likely another error will appear once this one is fixed.
The run1 - run3 buttons just bring up a window to select the excel file that I have already shared attached again. Currently all three will need to be filled with the same file.
Best Regards, Josh

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by