Using strjoin to put as a variable name in a table

10 ビュー (過去 30 日間)
Madalena Francisco
Madalena Francisco 2023 年 1 月 16 日
編集済み: Madalena Francisco 2023 年 1 月 16 日
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)

採用された回答

Steven Lord
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
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 件のコメント
Madalena Francisco
Madalena Francisco 2023 年 1 月 16 日
編集済み: Madalena Francisco 2023 年 1 月 16 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by