Convert Values From Cell Elements to String Elements for Complete Array

20 ビュー (過去 30 日間)
Jay
Jay 2019 年 6 月 22 日
編集済み: Jay 2019 年 6 月 24 日
Hi Guys,
BACKGROUND:
I have used the code:
[num,text,raw] = xlsread ('TestFile.xlsx')
to read in an Excel spreadsheet.
The "TestFile" has both string and numerical values in the array.
The created "raw" array (cell type) has all the information I believe required (providing I can convert form a cell type to a string type).
I will have the code add 1 to the instance count variable when there is a single "NaN' and end when there is 2 consecutive "NaN" (using an if-statement) later on.
Note: The data sets will be of the same structure but differ in the "NaN" placement (hence the instance count can't be hard coded).
QUESTION:
As the element placement of the "raw" array is how I would like (to access, peruse and then use the data), how do I convert or duplicate each element of the "raw" array so that its location is the same but of the string type (can numerical values be classified as strings for later numerical conversion [subroutine] prior to numerical use )?
Is it advisable to have one array as a string type and another array as the numeric type, then have a function read the type cell array and if a alphabetical, populate the numerical cell (same element position as the element in the cell type array), if alphabetical, populate the string array (again, same element position as the element in cell type array)?
That way I can peruse the relevant array types for their respective values?
NOTE:
I can’t use the "num" array since the element placement is dictated by the first instance of a number (required data for late use omitted).

採用された回答

Walter Roberson
Walter Roberson 2019 年 6 月 22 日
mask = cellfun(@ischar, raw);
raw(mask) = num2cell(string(raw(mask)));
  2 件のコメント
Jay
Jay 2019 年 6 月 22 日
編集済み: Jay 2019 年 6 月 22 日
Hi Walter,
I ran the code and it appears that it creates a Boolean array (true or false / 1 or 0) with the test being if a cell of the raw array has a value (cell type) in it.
I can't see how this helps facilitates the conversion from cell types to string and number types.
Walter Roberson
Walter Roberson 2019 年 6 月 22 日
編集済み: Walter Roberson 2019 年 6 月 22 日
The first line creates the boolean array of whether particular entries contain character vectors. The second line does the conversions and assigns the converted values back over the original ones.

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

その他の回答 (1 件)

Jay
Jay 2019 年 6 月 22 日
編集済み: Jay 2019 年 6 月 23 日
Hi Walter,
The "raw" array now has all alphabetical names as type text (" ").
Is there a command that does the same for numerical only values (opposed to alpha-numeric)?
I would like to create another array only with the numeric values from the raw array if possible?
Can you also please explain how the
num2cell(string(raw(mask)))
works to assign the string type to a cell array as the string specifier is prior to the Num2cell command?
  3 件のコメント
Walter Roberson
Walter Roberson 2019 年 6 月 23 日
raw(mask) is a cell array of character vectors. string() converts that to a nonscalar string object. num2cell on the nonscalar string object converts it to a cell array each entry of which is a scalar string object.
Jay
Jay 2019 年 6 月 24 日
編集済み: Jay 2019 年 6 月 24 日
Thank you Walter for the breakdown of the code and the code itself.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by