Inserting string into specific field in table

92 visualizaciones (últimos 30 días)
newbie9
newbie9 el 3 de Ag. de 2019
Respondida: Cris LaPierre el 3 de Ag. de 2020
I'm trying to insert a string into a specific cell in a table. I've tried a handful of combinations, including curly brackets, cellstr()... but can't seem to get the right combination. Stand-along code is below; thanks for any help, I am sure it is something simple that I am missing.
files = {'file1.txt', 'file2.txt', 'file3.txt'};
mytable = NaN(length(files), 4);
mytable = array2table(mytable);
mytable.Properties.VariableNames = {'FILENAME', 'valueA', 'valueB', 'valueC'};
for i = 1:length(files)
filename = char(files(i));
% do things
mytable.FILENAME{i} = filename;
% also save other values; these are all doubles, so they write to table easily
end
Here is the error message:
Unable to perform assignment because brace indexing is not supported for variables of
this type.
Here are some other combos I have tried:
mytable.FILENAME{i} = cellstr(filename);
Unable to perform assignment because brace indexing is not supported for variables of
this type.
mytable.FILENAME{i} = filename;
Unable to perform assignment because brace indexing is not supported for variables of
this type.
mytable.FILENAME(i,:) = filename;
Unable to perform assignment because the size of the left side is 1-by-1 and the size
of the right side is 1-by-9.
mytable.FILENAME(i,:) = cellstr(filename);
Conversion to double from cell is not possible.
  1 comentario
Cyril CRIER
Cyril CRIER el 3 de Ag. de 2020
Hi,
I had the same problem, and when I try this combination:
mytable.FILENAME(i,1) = filename; ("1" for the first column) instead of mytable.FILENAME(i,:) = filename;
I get a "NaN" value instead of the String chain I want to assign, but no Error anymore

Iniciar sesión para comentar.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 3 de Ag. de 2020
Table variables have an assigned data type. You must set the datatype to match the data you want to store. Filenames are text, not numbers.
files = {'file1.txt', 'file2.txt', 'file3.txt'};
mytable = NaN(length(files), 3);
mytable = [table(strings(length(files), 1)), array2table(mytable)];
mytable.Properties.VariableNames = {'FILENAME', 'valueA', 'valueB', 'valueC'}
for i = 1:length(files)
filename = char(files(i));
% do things
mytable.FILENAME(i) = filename
% also save other values; these are all doubles, so they write to table easily
end

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by