1.193 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
John am 6 Okt. 2023
Kommentiert: Dyuman Joshi am 6 Okt. 2023
Akzeptierte Antwort: Dyuman Joshi
Hello everyone, I've just started out with Matlab for uni and am trying to add a column to a table. I ran into the following error message
Error using .
To assign to or create a variable in a table, the number of rows must match the height of the table.
My code is written below:
%Creates a table called 'tab' using the data in the coronavirus-cases.csv
%file.
tab = readtable("coronavirus-cases.csv");
%Creates a row of zeros with the same height as tab.
newTabCol = zeros(height(tab), 1);
%Adds the column and titles it "Seven Day Average".
tab.newTabCol = "Seven Day Average";
What confuses me is that the height of newTabCol matches tab. Both have 153772 rows:
I'm sure I'm missing something basic but any help would be appreciated.
Kind regards,
John
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Akzeptierte Antwort
Dyuman Joshi am 6 Okt. 2023
The variable name/title should be used after using the dot notation inside parenthesis and the data to append should be on the right side of the equation.
%Creates a row of zeros with the same height as tab.
newTabCol = zeros(height(tab), 1);
%Adds the column and titles it "Seven Day Average".
tab.("Seven Day Average") = newTabCol;
2 Kommentare Keine anzeigenKeine ausblenden
Keine anzeigenKeine ausblenden
John am 6 Okt. 2023
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2030069-adding-a-column-to-a-table#comment_2913109
⋮
Thanks so much Dyuman!
Dyuman Joshi am 6 Okt. 2023
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2030069-adding-a-column-to-a-table#comment_2913114
You are welcome!
Also, do check out Image Analst's suggestion as well.
Melden Sie sich an, um zu kommentieren.
Weitere Antworten (1)
Image Analyst am 6 Okt. 2023
To add a column to a table, use addvars
help addvars
--- help for table/addvars --- ADDVARS Add variables to table or timetable. T2 = ADDVARS(T1, VAR1, ..., VARN) appends the arrays VAR1,...,VARN as variables to the table T1. VAR1,...,VARN can include arrays of any type, including tables and timetables. All input arguments must have the same number of rows. T2 = ADDVARS(..., 'Before', LOCATION) T2 = ADDVARS(..., 'After', LOCATION) inserts the variables either before or after the position specified by LOCATION. LOCATION is a positive integer, a variable name, or a logical vector containing a single true value. ADDVARS(..., 'After', width(T1)) is equivalent to the default behavior. T2 = ADDVARS(..., 'NewVariableNames', NEWNAMES) specifies the names of the variables added in T2. NEWNAMES is a string array or cell array containing the same number of names as the number of added variables. Examples: % Load a table load fatalities.mat % Add per-capita deaths to table fatalities = addvars(fatalities, ... fatalities.deaths./fatalities.drivers, ... 'NewVariableNames', 'deathsPerThousand', ... 'After', 'drivers'); See also REMOVEVARS, MOVEVARS, SPLITVARS, MERGEVARS.Help for table/addvars is inherited from superclass tabular Documentation for table/addvars doc table/addvars Other uses of addvars codistributed/addvars gpuArray/addvars tall/addvars distributed/addvars tabular/addvars
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
MATLABLanguage FundamentalsData TypesNumeric TypesLogical
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung