Using strjoin to put as a variable name in a table
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Steven Lord
2023-1-16
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!