NSK
NSK

Reputation: 1

converting a .mat file with table to a .mat file with cell array

I need to convert a .mat file that contains a variable 't' of the type table to a cell. This is because there is another code that reads a cell array and performs str2num and char functions on it. When the code uses the str2num and char functions on the table that needs conversion it throws an error " Error using char : Conversion to char from table is not possible ". Is there anyway I can convert the format of a .mat file(with a table) to the format of a different .mat file(containing cell arrays) so that the code written for the.mat files with cell arrays works for the .mat file with table?

Upvotes: 0

Views: 857

Answers (1)

atmb321
atmb321

Reputation: 11

I will try to answer this question. First let us create a table with numbers as cellstrings and save this table as a .mat file.

TestNumbersAsString = {'42', '500', '288'}';
t = table(TestNumbersAsString);
save('myTable.mat', 't');

Let us clear our workspace and load the file. convert the table to a cell and save this cell as a .mat file. Load this file again.

clearvars t
load('myTable.mat');
tAsCell = table2cell(t);
save('myCell.mat', 'tAsCell');
clearvars tAsCell
load('myCell.mat');

Convert and print the first element in the cell.

display(str2num(tAsCell{1}))

We could have used also the table.

display(str2num(t{1,1}{1}))

EDIT: please share an example and explain what you really want. You have never mentioned the column labels or NaN values.

tAsCellWithLabel = [t.Properties.VariableNames; table2cell(t)];

And you can remove the NaN values or check for NaN values and never call the function "str2num", if the cell value is NaN. Check the "isnan" function. There are so many ways to solve it. But I do not know whats your data and how you want to use the data.

Upvotes: 1

Related Questions