Using strjoin to put as a variable name in a table

10 visualizaciones (últimos 30 días)
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)

Respuesta aceptada

Steven Lord
Steven Lord el 16 de En. de 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
A1 = 3×3 table
var1 var2 var3 ____ ____ ____ 8 1 6 3 5 7 4 9 2
A2 = array2table(A, VariableNames=string(N)) % string array
A2 = 3×3 table
var1 var2 var3 ____ ____ ____ 8 1 6 3 5 7 4 9 2
N2 = cellfun(@string, N, UniformOutput=false)
N2 = 1×3 cell array
{["var1"]} {["var2"]} {["var3"]}
A3 = array2table(A, VariableNames=N2) % cell array of string scalars
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.
  1 comentario
Madalena Francisco
Madalena Francisco el 16 de En. de 2023
Editada: Madalena Francisco el 16 de En. de 2023
Ohhh thank thank! But I already create another version of my code and it´s working!! I convert the strjoin to chars using convertStringsToChars
But thanks a lot anyways! And yes yes it´s so important what you said thank uu

Iniciar sesión para comentar.

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