In a table, when I try assigning a value to a new column based on some criteria, I get error that "assignment to elements using simple assignment statement is not supported"
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jorge
el 16 de Sept. de 2024
Comentada: Jorge
el 17 de Sept. de 2024
I have a table for which I create an index where the values in a particular column are not NaN. I then try to use this index to create an entry into a new array and replace the values at the corresponding index (preloaded with '0000') with '0001'. I will then add this array to the table as a new column.
My intention is to fill the corresponding indx in the column with '0001', if the value in the newTable.PEM1_GA_Current_Estimate_Error a valid number (not NaN).
PEM1b ='0001';
PEM2b ='0010';
PEM3b = '0100';
PEM4 = '1000';
temp={};
indx=(find(newTable.PEM1_GA_Current_Estimate_Error ~= NaN))
temp ={repmat('0000',size(newTable,1),1)}
temp{indx} = PEM1b
%...and add this array as a new column to the table
the last assigment in the code above generates this error:
Assigning to 2609 elements using a simple assignment statement is not supported. Consider using comma-separated list assignment.
I don't think I should need a for loop to iterate through each row and replace the value at the "indx" location --what am I doing wrong?
I included the data....thank you.
3 comentarios
Voss
el 16 de Sept. de 2024
newTable = load('data.mat').newTable
PEM1b = '0001';
indx = ~isnan(newTable.PEM1_GA_Current_Estimate_Error);
temp = repmat({'0000'},size(newTable,1),1);
temp(indx) = {PEM1b}
%...and add this array as a new column to the table
newTable.newColumn = temp
Respuesta aceptada
Stephen23
el 16 de Sept. de 2024
Editada: Stephen23
el 16 de Sept. de 2024
"I don't think I should need a for loop to iterate through each row and replace the value at the "indx" location"
You do not need a loop, just assign a scalar cell array to the existing cell array using parentheses:
temp(indx) = {PEM1b};
"what am I doing wrong?"
You are trying to assign one array to multiple cells of the cell array. You could think of it something like this:
[a,b,c,d,e,f,..] = pi
While this might be a defined syntax in some languages, with MATLAB this does not assign one array to lots of cells (or variables or fields or ...). In general it is an error. You could use DEAL for some of those situations, but your usecase is actually much simpler: you can use the special case that applies for any array type: a scalar on the RHS will always be expanded to fit the indexing on the LHS into an array of the same type. So all you need is a scalar cell array on the RHS. Which means that the LHS must use parentheses to assign to the cell array itself, not curly braces to replace its content. Then you assign a scalar array element on the RHS to as many elements on the LHS as the indexing specifies (note how I did not write cell, because this concept is exactly the same for every array class).
8 comentarios
Stephen23
el 17 de Sept. de 2024
Editada: Stephen23
el 17 de Sept. de 2024
"which seemed to work. But I didn't understand why it would...."
Unlike some other languages, type coercion in MATLAB was in the past basically limited to the numeric types and char. For better or for worse, modern users apparently expect type coercion between everything, so the (relatively new) string class can coerce numeric, char, and cell-of-char into strings:
S = "this" % string
S(2) = 'assigns' % char
S(3) = 3 % numeric
S(4) = {'types'} % cell of char
"which to me makes more sense. can you explain what I "did" here?"
You created a string array and then assigned a character vector to it, which got coerced into a string:
T = ["hello";"x"]
T(2) = 'world'
Using a scalar string on the RHS would not use any coercion.
Más respuestas (0)
Ver también
Categorías
Más información sobre Multidimensional Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!