how to use setvartype on a table that is already existing
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (4)
Simon Chan
il 25 Lug 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 Commenti
Robert
il 25 Lug 2021
Robert
il 25 Lug 2021
Walter Roberson
il 25 Lug 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.
Robert
il 25 Lug 2021
0 voti
Walter Roberson
il 26 Lug 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:
- You want to create a 2 x 200 table in which every variable is character array of width 0
- 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)
- You want to create a 2 x 200 table in which every variable is a string
- You might want to convert existing table variables into char array -- this one is difficult
- 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 Commenti
Walter Roberson
il 26 Lug 2021
Modificato: Walter Roberson
il 26 Lug 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
il 27 Lug 2021
Peter Perkins
il 26 Lug 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 Commenti
Walter Roberson
il 26 Lug 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
il 27 Lug 2021
"If you are trying to make a raw char variable in a table, don't do that."
Categorie
Scopri di più su Matrices and Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!