フィルターのクリア

Existing table, populate a column with string (receiving errors)

23 ビュー (過去 30 日間)
newbie9
newbie9 2019 年 11 月 18 日
コメント済み: Cris LaPierre 2022 年 9 月 19 日
I have a table, and I want to populate every row in a specific column with the same string. I keep getting errors. Using Matlab R2018a, no fancy packages.
mytable = nan(5,2);
% I'm creating my table like this; it's actually much bigger, and most of the columns are numerical
% is there a better way to do this? the order of the column matters, and I don't want to have to add extra code to re-order
mytable = array2table(mytable);
mytable.Properties.VariableNames = {'colA', 'colB'};
mytable.colA(:) = 459; % just a random number, proof-of-concept that it works
mytable.colB(:) = {'words'}; % this throws errors
  2 件のコメント
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa 2022 年 9 月 18 日
移動済み: the cyclist 2022 年 9 月 18 日
I have something similar, I have a master table with more then 270k lines and 85 columns. I added to my master table one column with subject Id of my participants, wich I get from the name of my first level of subfolders, but I am not being able to populate every line with the subject Id, only the first line is being populated, can you help on that??? @Cris LaPierre or anyoner expert???
clear
clc
mytopfolder = '/Users/Participants data';
filepattern = fullfile(mytopfolder, '**/VIVE/Walking_ST/Trial_*.txt');
thefiles = dir(filepattern);
nfiles = length(thefiles);
for k = 1:nfiles
basefilename = thefiles(k).name;
fullfilename = fullfile(thefiles(k).folder, basefilename);
fprintf(1, 'Now reading %s\n', fullfilename);
thisTable = readtable(fullfilename);
thisTable.Subject_Id(k,:) = fullfilename(98:102);
fprintf(' It has %d rows in it.\n', height(thisTable));
if k == 1
masterTable = thisTable;
else
masterTable = [masterTable; thisTable];
end
fprintf(' Now there are a total of %d rows in the master table.\n', height(masterTable));
end
Cris LaPierre
Cris LaPierre 2022 年 9 月 19 日
This is probably best asked as a new question, as more people will see it. Quick observation, though, is that you are assigning the Subject_ID to the kth row, all columns. Perhaps you meant to assign to all rows?

サインインしてコメントする。

採用された回答

Cris LaPierre
Cris LaPierre 2019 年 11 月 18 日
You are close, but you are trying to assign a character vector to a double variable (NaN = not a number). You can see that if you run the command summary(mytable).
You also do not need to put the text into a cell if you use the string data type.
See if you can get this code to work for your needs.
myArray = nan(5,2)
mytable = array2table(myArray);
mytable.Properties.VariableNames = {'colA', 'colB'};
mytable2 = convertvars(mytable,"colB",'string');
mytable2.colA(:) = 459;
mytable2.colB(:) = "words"
You say you have a lot of data. If the variables already exist, you could do something like this:
colA = nan(5,1);
colB = strings(5,1);
mytable = table(colA,colB);
mytable.colA(:) = 459; % just a random number, proof-of-concept that it works
mytable.colB(:) = "words" % this throws errors
  4 件のコメント
newbie9
newbie9 2020 年 3 月 31 日
this works in R2018a, thank you!
Felix Knödl
Felix Knödl 2022 年 8 月 5 日
Try this:
mytable.colB(:) = {"words"}

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by