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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by