how to use setvartype on a table that is already existing

40 Ansichten (letzte 30 Tage)
Robert
Robert am 25 Jul. 2021
Kommentiert: Peter Perkins am 27 Jul. 2021
i am struggling with tables very badly.
I can not find a single shred of information on how to use setvartype function on an existing table
All the examples do it when reading in a table from a file.
I created a table in my code using
cell2table(cell(2,80))
and it gave me a 2X 80 table
However it wont let me put anything in it because the variales are not the correct type
Can someone tell me how to set all the variable types to one uniform type using setvartype function?
I can only find example that do it when importing a new table from a file not on an existing new file
the main problem im having is.... im trying to put data in the table
and im getting the following error
"conversion to cell from char is not possible"
The data im trying to put in the table location is of the type char
What am i doing wrong.... Can someone please lend a hand

Akzeptierte Antwort

Jeremy Hughes
Jeremy Hughes am 25 Jul. 2021
Bearbeitet: Jeremy Hughes am 25 Jul. 2021
This will do what you're literally asking for.
T = array2table(strings(200,0)) % 200 variables, 0 rows, all string type
T = 200×0 empty table
But I have a feeling it would be easier to give you the best answer with a bit more context.
From the small amount of code you submitted, I tried this:
T = cell2table(cell(2,80))
T = 2×80 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Var25 Var26 Var27 Var28 Var29 Var30 Var31 Var32 Var33 Var34 Var35 Var36 Var37 Var38 Var39 Var40 Var41 Var42 Var43 Var44 Var45 Var46 Var47 Var48 Var49 Var50 Var51 Var52 Var53 Var54 Var55 Var56 Var57 Var58 Var59 Var60 Var61 Var62 Var63 Var64 Var65 Var66 Var67 Var68 Var69 Var70 Var71 Var72 Var73 Var74 Var75 Var76 Var77 Var78 Var79 Var80 ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
T(1,1) = {'SomeData'} % Need to insert a cell.
T = 2×80 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Var25 Var26 Var27 Var28 Var29 Var30 Var31 Var32 Var33 Var34 Var35 Var36 Var37 Var38 Var39 Var40 Var41 Var42 Var43 Var44 Var45 Var46 Var47 Var48 Var49 Var50 Var51 Var52 Var53 Var54 Var55 Var56 Var57 Var58 Var59 Var60 Var61 Var62 Var63 Var64 Var65 Var66 Var67 Var68 Var69 Var70 Var71 Var72 Var73 Var74 Var75 Var76 Var77 Var78 Var79 Var80 ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ {'SomeData'} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
Which works.
"However it wont let me put anything in it because the variales are not the correct type"
Are you trying to do this:
T(1,1) = 'SomeData'
The number of table variables in an assignment must match.
If not, can you show the example code that you tried to use?
(edited to run the code!)
  7 Kommentare
Peter Perkins
Peter Perkins am 26 Jul. 2021
Walter, dot is not exactly syntactic sugar for braces (and I am always puzzled by the number of people who use braces for accessing one variable -- I find dot far more readable). Consider:
>> t = table([1;2;3],["AAA";"BB";"C"])
t =
3×2 table
Var1 Var2
____ _____
1 "AAA"
2 "BB"
3 "C"
Surely you would not want this assignment
>> t{1:2,1} = single([11;12])
t =
3×2 table
Var1 Var2
____ _____
11 "AAA"
12 "BB"
3 "C"
to turn Var1 into a single, and it does not -- it converts the RHS to the LHS, just as it would if these were arrays directly in the workspace. So neither should
>> t{:,1} = single([21;22;23])
t =
3×2 table
Var1 Var2
____ _____
21 "AAA"
22 "BB"
23 "C"
convert Var1, despite the :. This is where dot comes in: you can dot assign into the variable using parens
>> t.Var1(:) = single([21;22;23])
t =
3×2 table
Var1 Var2
____ _____
21 "AAA"
22 "BB"
23 "C"
but if you leave off any paren subscripting
>> t.Var1 = single([21;22;23])
t =
3×2 table
Var1 Var2
____ _____
21 "AAA"
22 "BB"
23 "C"
then you are overwriting the whole variable. Pretty sure this is in the doc but maybe it's easy to miss. I'll make a note about that.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Simon Chan
Simon Chan am 25 Jul. 2021
Please refer to the following example without using setvartype.
(1) When you create the table, you can create the preset variable names and variable types like the following, noticed that type char may not be supported but you may use type cell. On the other hand, you can assign the size of the table and my example shows a table with size 50 rows and 3 column.
(2) Suppose your data is stored in variable and the following code put the first 15 rows of data from variable into table T.
T.Properties.VariableNames = {'Name','Class','Value'};
T = table('Size',[50 3],'VariableTypes',{'cell','cell','double'})
T(1:15,:) = variable(1:15,:);
  3 Kommentare
Robert
Robert am 25 Jul. 2021
and by the way, your code does not run, it errors out
Why post code on here that doesnt work
Walter Roberson
Walter Roberson am 25 Jul. 2021
Reminder: In the great majority of cases, people who respond on MATLAB Answers are doing so as volunteers. That includes the great majority of cases where Mathworks Staff respond: nearly the only people who respond here as part of their job are the people who run MATLAB Central itself, such as staff responding about bugs in the MATLAB Answers facility.
And volunteers contribute as much or as little as they feel like, and do whatever testing (or non-testing) they feel like.
You have been around for years, Robert, and by now the regular volunteers have largely formed the opinion that you are competent enough that you only need guideance on approaches and can work from there, rather than you needing fully-tested code due to you being inexperienced. Perhaps the volunteers are mistaken about that point and have greatly over-estimated your skills. If so, then it would help the volunteers if you were mention if you are not an experienced worker who is able to make projections from incomplete information; doing that would help alleviate perceptions that are perhaps being formed that you might be demanding more than is reasonable from volunteers.

Melden Sie sich an, um zu kommentieren.


Robert
Robert am 25 Jul. 2021
Does anyone know how to do this? I am at a complete standstill
All i want to do is create a table of one variable type! Why is everything so hard in matlab. Its rediculous
create a table of x,y size of a single variable type without having to address every single type individualy, im not going to type the word 'string' 200 times for a table that is 200 wide with all strings.
This is utterly rediculous. This is why kids today are learning python not matlab

Walter Roberson
Walter Roberson am 26 Jul. 2021
Can someone tell me how to set all the variable types to one uniform type using setvartype function?
That is not possible. setvartype() applies only to options structures such as are created by detectImportOptions() and related direct constructors.
The data im trying to put in the table location is of the type char
cell2table(cell(2,80))
Those would be empty cells. If you were to suceed in converting them to char, then the result would be a table full of 2 x 0 char arrays, which you would not be able to store into.
All i want to do is create a table of one variable type!
Are you changing your question? Your original question is about converting existing table variables to datatype char. MATLAB discourages using char variables in tables, recommending that you instead use cell array of character vector or string() objects -- when you use char variables then the fields are fixed width (just like char arrays). MATLAB deliberately makes it difficult to convert variables to char array.
If you want to create a table of all char variables, that is a quite different matter than converting existing variables.
im not going to type the word 'string' 200 times for a table that is 200 wide with all strings.
Please clarify whether you are needing the entires to be char (fixed-width fields) or string (variable width, additional methods).
I see several possibilities here:
  1. You want to create a 2 x 200 table in which every variable is character array of width 0
  2. You want to create a 2 x 200 table in which every variable is a character array of a particular non-zero width (e.g., 5 characters wide for all of them)
  3. You want to create a 2 x 200 table in which every variable is a string
  4. You might want to convert existing table variables into char array -- this one is difficult
  5. You might want to convert existing table variables into string objects
I have code solutions for all of these (but #4 is substantially more complicated, and I would rather not post that unless you have a specific need to use char arrays as table objects).
Normally I would have gone ahead and posted some solutions, but considering your remarks to Simon about not posting code that does not work, I need to know exactly what your needs are so that I can post code that will work for your needs.
  3 Kommentare
Walter Roberson
Walter Roberson am 26 Jul. 2021
Bearbeitet: Walter Roberson am 26 Jul. 2021
You asked why post code that doesn't work. In order to be sure that any code I might post would work for your situation, I need clarification from you as to what your situation is. Why would I post code that doesn't work for your purpose?
Note by the way that I am the only person who answered the question you originally asked.
Robert
Robert am 27 Jul. 2021
@Walter Roberson and i appreciate your effort and skills however your delivery could use some work.
Your right i have been around a while and im a big fan of matlab. However, in my line of work i am subjected to using it extensivley for 6 months then not touching it for 4 years and then coming back. I am jealous of your everyday exposure to my favorite program.

Melden Sie sich an, um zu kommentieren.


Peter Perkins
Peter Perkins am 26 Jul. 2021
setvartype is an import feature. let's come back to that.
cell creates a cell array containing nothing but 0x0 doubles:
>> c = cell(3,2)
c =
3×2 cell array
{0×0 double} {0×0 double}
{0×0 double} {0×0 double}
{0×0 double} {0×0 double}
cell2table tries to create a table the same size as c, by vertcat'ing the contents of each cell in each column of the cell array. But those contents are all 0x0, so it can't do that. So it falls back to leaving every column of c as a cell array in t (this is all in the doc):
>> t = cell2table(c)
t =
3×2 table
c1 c2
____________ ____________
{0×0 double} {0×0 double}
{0×0 double} {0×0 double}
{0×0 double} {0×0 double}
And that's probably not what you want, because you won't be able to do anything useful, unless all you have is text data. Even if all you have is text data, you are not doing yourself a favor by using cell arrays instead of string arrays, but let's come back to that. The reason why you can't assign into those variables in the table is (I guess? I have not read through the entire thread) because they are cell arrays and you are (I guess) assigning as if it were something else. If you really do have only text data, and you are tyrig to assign a char row into the table, you need to remember that you are assigning into cell vars in the table, and the RHS should be a cell:
>> t(1,1) = {'abc'}
t =
3×2 table
c1 c2
____________ ____________
{'abc' } {0×0 double}
{0×0 double} {0×0 double}
{0×0 double} {0×0 double}
This is not all that convenient. So use dot to assign to one element of one var in the table. The c1 var is a cell array, how would you assign into it if it were directly in the workspace? You say c1{2} = 'def'. Do that, in the table:
>> t.c1{2} = 'def'
t =
3×2 table
c1 c2
____________ ____________
{'abc' } {0×0 double}
{'def' } {0×0 double}
{0×0 double} {0×0 double}
Still, it's easier by far to use strings, because no braces:
>> s = strings(3,2)
s =
3×2 string array
"" ""
"" ""
"" ""
>> t = array2table(s)
t =
3×2 table
s1 s2
__ __
"" ""
"" ""
"" ""
>> t.s1(2) = "def"
t =
3×2 table
s1 s2
_____ __
"" ""
"def" ""
"" ""
OK, back to the original question. You've asked about setvartype, which made me think you were really asking about import. But I think that's a red herring. To create a table all of whose variables are one type, use the syntax specifically for that:
>> t = table('Size',[3 2],'VariableTypes',repmat("double",1,2))
t =
3×2 table
Var1 Var2
____ ____
0 0
0 0
0 0
Maybe in your case, you want t = table('Size',[3 2],'VariableTypes',repmat("string",m,n)). Perhaps using repmat with VariableTypes ought to be a doc example, I'll make a note of that.
The other question, about changing the type of a variable in a table: any assignment syntax that involves parens or brace subscripting on the table is by definition assigning INTO the variables in the table. Only a dot assignment, with no subscripts, can change the type. It's equivalent to deleting and replacing. The other alternative is the convertvars function. But I'm not even sure what type you want to change from or to. If you are tyring to make a raw char variable in a table, don't do that, it is far more awkward that either cell arrays or (the best choice) string arrays.
  2 Kommentare
Walter Roberson
Walter Roberson am 26 Jul. 2021
convertvars() has a specific built-in limitation that prevents@char from being used as the datatype, substituting in @cellstr instead. One warning is given for each variable for which @cellstr is substituted for @char .
table() also has a specific built-in limitation that prevents 'char' from being used as the VariableTypes. One warning is given for each variable for which cell is substituted for char.
(I do wonder if it is necessary to issue one warning for each variable, as opposed to one warning for it happening at all.)
Peter Perkins
Peter Perkins am 27 Jul. 2021
"If you are trying to make a raw char variable in a table, don't do that."

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by