Adding Text To Table Data

10 visualizzazioni (ultimi 30 giorni)
Ben van Zon
Ben van Zon il 14 Set 2023
Commentato: Ben van Zon il 14 Set 2023
I wanna give names to transitions in my file, but whenever I want to name something it changes to a numerical value or NaN.
So whenever the value is -1 -> P, 0 -> Q and 1 -> R.
FileName = 'ct4004355_si_003.txt';
T = readtable('ct4004355_si_003.txt', 'Delimiter','\t', 'TextType','string');
Rows = T(130:154,[1 2 3 4 5 6 11 12 13 14]);
Rows.Properties.VariableNames = ["Wavenumber [cm-1]", "Uncertainty", "v1u", "v2u", "L2u", "Ju", "v1l", "v2l", "L2l", "Jl"];
PR = Rows.Ju-Rows.Jl;
Rows = [Rows table(PR, 'VariableNames', {'Branch'}) ];
Rows.Branch(ismember(Rows.Branch, [-1])) = 'P'
  1 Commento
Ben van Zon
Ben van Zon il 14 Set 2023
25×11 table
Wavenumber [cm-1] Uncertainty v1u v2u L2u Ju v1l v2l L2l Jl Branch
_________________ ___________ ___ ___ ___ __ ___ ___ ___ __ ______
2051.5 0.01 0 1 1 5 0 0 0 6 80
2054 0.01 0 2 2 5 0 1 1 6 80
2057.4 0.01 0 2 0 1 0 1 1 2 80
2060.2 0.01 0 1 1 6 0 0 0 7 80
2061.7 0.01 0 1 1 5 0 0 0 6 80
: : : : : : : : : : :
2134.2 0.01 0 2 0 7 0 1 1 7 0
2134.9 0.01 0 1 1 4 0 0 0 5 80
2137 0.01 0 2 2 4 0 1 1 5 80
2140.3 0.01 0 1 1 4 0 0 0 5 80
2142.3 0.01 0 2 2 4 0 1 1 5 80
This is the result, instead of '80' I need a 'P'.

Accedi per commentare.

Risposta accettata

Dyuman Joshi
Dyuman Joshi il 14 Set 2023
FileName = 'ct4004355_si_003.txt';
T = readtable('ct4004355_si_003.txt', 'Delimiter','\t', 'TextType','string');
Rows = T(130:154,[1 2 3 4 5 6 11 12 13 14]);
Rows.Properties.VariableNames = ["Wavenumber [cm-1]", "Uncertainty", "v1u", "v2u", "L2u", "Ju", "v1l", "v2l", "L2l", "Jl"];
PR = Rows.Ju-Rows.Jl;
Rows = [Rows table(PR, 'VariableNames', {'Branch'}) ];
str = 'PQR';
Rows.Branch = str(Rows.Branch+2)'
Rows = 25×11 table
Wavenumber [cm-1] Uncertainty v1u v2u L2u Ju v1l v2l L2l Jl Branch _________________ ___________ ___ ___ ___ __ ___ ___ ___ __ ______ 2051.5 0.01 0 1 1 5 0 0 0 6 P 2054 0.01 0 2 2 5 0 1 1 6 P 2057.4 0.01 0 2 0 1 0 1 1 2 P 2060.2 0.01 0 1 1 6 0 0 0 7 P 2061.7 0.01 0 1 1 5 0 0 0 6 P 2067.4 0.01 0 1 1 6 0 0 0 7 P 2074 0.04 0 2 2 5 0 1 1 6 P 2077.5 0.01 0 1 1 6 0 0 0 7 P 2079.4 0.01 0 1 1 5 0 0 0 6 P 2080.7 0.03 0 1 1 6 0 0 0 7 P 2089.3 0.01 1 1 1 3 1 0 0 4 P 2089.8 0.01 0 2 2 5 0 1 1 6 P 2094.2 0.015 0 2 2 5 0 1 1 6 P 2095.3 0.02 0 2 2 4 0 1 1 5 P 2096.6 0.01 0 1 1 5 0 0 0 6 P 2097.7 0.01 1 1 1 3 1 0 0 4 P
  3 Commenti
Dyuman Joshi
Dyuman Joshi il 14 Set 2023
> Explaination - When you convert the data type of a portion of data, (if the conversion is allowed) MATLAB retains the data type and changes the modified data to the original data type.
%Double numeric
y = 1:5
y = 1×5
1 2 3 4 5
z = 'Yo'
z = 'Yo'
%Last 2 values changed to string, but the MATLAB converts the corresponding
%string to double numeric
y(end-1:end) = z
y = 1×5
1 2 3 89 111
%Character vector
y = 'Maths is fun'
y = 'Maths is fun'
%1st 3 values changed to numeric, MATLAB converts the
%corresponding numbers to their character (ASCII) values
y(1:3) = [69 420 666]
y = 'EƤʚhs is fun'
If you want to change the data-type of variable corresponding to some value, over-write the whole array -
y = 1:5;
y = sprintf('%d', y)
y = '12345'
In your case, the original data was numeric. You modified some values to be a character ('P' to be exact), MATLAB converted it to the numerical value
double('P')
ans = 80
I over-wrote the whole data.
> P = -1, Q = 0, R = 1
can be changed to
P+2 = 1, Q+2 = 2, R+2 = 3
So, I used 1, 2, and 3 as indices, corresponding to 'P', 'Q' and 'R'.
Why change? Because indexing in MATLAB starts with 1.
> What if the values were different and the data was much bigger? Say -
P = -53, Q = 13, R = 0
%vec is only used to generate data
vec = [-53 0 13];
str = 'PQR';
%And this is the data we are working with
data = vec(randi(3,1,100))
data = 1×100
0 0 0 0 -53 -53 -53 -53 13 13 -53 13 0 0 -53 0 -53 0 13 0 0 13 -53 0 13 -53 0 13 13 0
%Then get the unique values and corresponding indices
[arr,~,idx] = unique(data)
arr = 1×3
-53 0 13
idx = 100×1
2 2 2 2 1 1 1 1 3 3
%And use the indices to change the values
data = str(idx)
data = 'QQQQPPPPRRPRQQPQPQRQQRPQRPQRRQPRRQQQQPQPPPRRPRRPRRRQQRQPRQQPRRQRPRPRQPRQQRQPQQPQRRRQRQPPQRQQPQRRQPQR'
Ben van Zon
Ben van Zon il 14 Set 2023
Thank you for the extensive answer!
This will be useful for the other things I'm trying to do with this table.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by