Unable to call table2array in another function

Hi,
I have a table with only 1 column containing numeric data.
filename = 'dataset_Facebook_modified.csv';
data = readtable(filename);
labels = data(:,end);
Then I want to create a function called 'l_std' where I'm trying to use table2array to carry out some calculations.
function [labels_std, labels_avg] = l_std(labels)
labels_std = 0;
labels_avg = 0;
label = table2array(labels);
labels_std = std(label)
labels_avg = mean(label)
end
However, I got the error message as below (red text):
Brace indexing is not supported for variables of this type.
Error in table2array (line 27)
a = t{:,:};
Error in l_std (line 5)
label = table2array(labels);
When I run it in the main tab, it works fine. I just cannot run it in another function (in another tab).
Can anyone advise where is the error and how to solve it?

 採用された回答

M.Many
M.Many 2020 年 11 月 28 日
編集済み: M.Many 2020 年 11 月 28 日

0 投票

labels = data{:,end};
Use this to directly get labels as an array instead of a table. There is no need to use table2array.

10 件のコメント

Walter Roberson
Walter Roberson 2020 年 11 月 28 日
Note that if you do this, then the function should not do the table2array() step.
Hanisah Rashid
Hanisah Rashid 2020 年 11 月 28 日
Thank you for the answer. But just in case I need to use the variable in the main tab as a table, what causes the error of calling table2array in another function?
M.Many
M.Many 2020 年 11 月 28 日
The function l_std just works fine on my side. Maybe the error comes from the table data. Could you paste data(:,end) ?
Walter Roberson
Walter Roberson 2020 年 11 月 28 日
what causes the error of calling table2array in another function?
The other function is not passing in a table.
Go to the command line and command
dbstop if error
and then run your function in the other tab (whatever that means to you.) When the problem occurs, give the command
dbup
and then look at class(labels)
Hanisah Rashid
Hanisah Rashid 2020 年 11 月 28 日
Attached is the result of labels as a table.
Walter Roberson
Walter Roberson 2020 年 11 月 28 日
And when you did the dbstop and so on, what showed up as class(labels) inside the function ??
M.Many
M.Many 2020 年 11 月 28 日
編集済み: M.Many 2020 年 11 月 28 日
Again the function just works fine with labels on my side. You can add a breakpoint just before the table2array command
function [labels_std, labels_avg] = l_std(labels)
labels_std = 0;
labels_avg = 0; %<-- ADD BREAKPOINT HERE (The execution will stop before this command)
label = table2array(labels);
labels_std = std(label)
labels_avg = mean(label)
end
Run the code and then type in the Command window
whos labels
to check if labels is a table
Hanisah Rashid
Hanisah Rashid 2020 年 11 月 29 日
hi both, I did the dbstop if error and dbup to find the class for labels, and apparently I got it as double. However, when I remove the table2array line and change the next to functions to std(labels) and mean(labels), I got the error to run the std function instead due to invalid data type..
Hanisah Rashid
Hanisah Rashid 2020 年 11 月 29 日
I understand that I can set the initial variables directy to arrays and change back to table when I needed it, but I'm just stumped figuring out where I got it wrong.
Walter Roberson
Walter Roberson 2020 年 11 月 29 日
We do not know, as we have not seen the code for that other "tab".
I am not even clear what a "tab" is for this purpose?

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWorkspace Variables and MAT Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by