Using strjoin to put as a variable name in a table
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Madalena Francisco
le 16 Jan 2023
Modifié(e) : Madalena Francisco
le 16 Jan 2023
Hi, my main question is: how to concatenate 2 input strings, turn it into a single string, and put it as a variable name in a table?
I'm trying to make the input of the independent variable (vi) stay together with the units(vi_u) when the table appears,
for example: time_s
and the same for the independent variable
I created isolated strings, then joined them as a single string, to achieve my goal, but I get this error:
%Error using array2table
%The VariableNames property is a cell array of character vectors. To assign multiple variable names, specify nonempty names in a string array or a cell array of character
%vectors.
%table = array2table(a,'VariableNames', {string(strjoin(tabc1,"_")),string(strjoin(tabc2,"_"))});
This is my code:
vd=input(['Indicate the name of the quantity corresponding to the variable ' ...
'dependents');
d1= sprintf('Indicate the units of the variable %s: ',vd);
vd_u=input(d1,'s');
tabc2= {vd vd_u};
tab_c2=string(strjoin(tabc2,"_"));
clc
table = array2table(a,'VariableNames', {tab_c1,tab_c2});
disp(table)
0 commentaires
Réponse acceptée
Steven Lord
le 16 Jan 2023
Specify VariableNames as either a cell array containing char vectors or as a string array. Don't specify them as a cell array containing string scalars.
A = magic(3);
N = {'var1', 'var2', 'var3'};
A1 = array2table(A, VariableNames=N) % cell array of char vectors
A2 = array2table(A, VariableNames=string(N)) % string array
N2 = cellfun(@string, N, UniformOutput=false)
A3 = array2table(A, VariableNames=N2) % cell array of string scalars
1 commentaire
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!