I have a cell B of 1x59 strings in it and i want to insert it into a table T as variable names
When am using the command
T.VariableNames.Properties = B; am getting the following error
"The VariableNames property is a cell array of character vectors. To assign multiple variable names, specify names in a
string array or a cell array of character vectors."
I checked multiple times by checking whether the cell is str from command iscellstr and it returns me 1.
Can anyone please help how to solve this problem

1 件のコメント

Walter Roberson
Walter Roberson 2021 年 4 月 22 日
You cannot use a cell array of strings. You need to use a cell array of character vectors, or you need to use a non-scalar string() array.

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

 採用された回答

Stephan
Stephan 2021 年 4 月 22 日
編集済み: Stephan 2021 年 4 月 22 日

0 投票

The solution is inside the error message:
  1. B is a cell array of strings
  2. specify names in a string array or
  3. cell array of character vectors
What you have is 1. and what you need is either 2. or 3.
A = {"Name1", "Name2", "Name3"}
A = 1×3 cell array
{["Name1"]} {["Name2"]} {["Name3"]}
A_new = [A{:}]
A_new = 1×3 string array
"Name1" "Name2" "Name3"

7 件のコメント

Sandeep Nair
Sandeep Nair 2021 年 4 月 22 日
Am converting the cell to character vectors by using the command cellstr(B); But still the same error is showing,i tried your method as well but still the same error message
Stephan
Stephan 2021 年 4 月 22 日
can you provide the code you are using?
Sandeep Nair
Sandeep Nair 2021 年 4 月 23 日
編集済み: Walter Roberson 2021 年 4 月 23 日
Am using the following code
clc;
clear all;
[fname,fpath] = uigetfile({'*.xls';'*.xlsx'});
[num,txt,raw] = xlsread( fullfile(fpath,fname),'Sheet1' );
T=array2table(num);
B= cellstr(txt);
T.Properties.VariableNames= B;
Please find the code that am using
Stephan
Stephan 2021 年 4 月 23 日
Could you also attach the data needed to run the code?
Walter Roberson
Walter Roberson 2021 年 4 月 23 日
txt will already be a cell array of character vectors (unless possibly if it is empty).
Could you remind us which MATLAB release you are using?
Sandeep Nair
Sandeep Nair 2021 年 4 月 23 日
Thanks for the answers,i was able to find the solution i checked my cells with isvarname and some of the cells were having dots in between which i removed and now its working fine ,thanks
Walter Roberson
Walter Roberson 2021 年 4 月 23 日
Variable names with dots in them are permitted, R2019b and later (if I remember correctly.) You did not happen to indicate which release you are using.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File 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