Table element access problem

조회 수: 1 (최근 30일)
Alex K
Alex K 2023년 5월 8일
댓글: Alex K 2023년 5월 11일
I am reading a table from an excel file
T_Log_Pf = readtable(Log_Pf_path_fn);
But the compiler generates mxArray instead of a table, and when I try to change the element
T_Log_Pf{1, {'sts_e'}} = 1;
or get value of element from the table
x = T_Log_Pf.sts_e(1);
the error "Cell contents reference from a non-cell array object" occurs.
What and how should be corrected in order to work with the table?
Ore the problem is in kompilator setup?
  댓글 수: 28
Alex K
Alex K 2023년 5월 10일
@dpb, thanks for the suggested options!
And information about <executing extrinsics> was very useful.
I decided not to experiment with the table anymore, because any creation of a predefined table will still go through an external function table and therefore again will give mxArray and new problems.
I was quite satisfied with the solution with converting the table into an array with a fixed size, which is predefined.
With the variable names for the table, I decided to declare a global array with variable names. Because there are only several types of tables, I do not expect large expenses for their administration during the development of the model. Additionally, it is possible to guarantee the unity of table formats formed in different modules.
Alex K
Alex K 2023년 5월 11일
@dpb, colleagues, hello!
In continuation of the question with the table, a few more appeared:
1. How to initialize an array with names of table column using m-file?
(e.g. { ' var1' ' var2' ' var3'} )
I initiate the model variables through an m-file which I run before running the model in Simulink. I would like to initiate this array in the same file.
To initiate variables, I use the sequence:
test_const = Simulink.Signal;
test_const.DataType = 'double';
test_const.Dimensions = 1;
test_const.Complexity = 'real';
test_const.SamplingMode = 'Sample based';
test_const.InitialValue = '0';
I would like to use something like this.
2. How to make a constant out of this variable?
It is recommended to set constants through functions.
But there can be many loads of the tables, and the simulation time must be reduced. Therefore, I don’t really want to waste time on the output of an external function, + an external function can cause problems with mxArray.
I found an option to frose the constant by using comand
test_const.Clocked = 'on';
but this command did not work for me.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 5월 9일
Use this standard syntax to read the data from MS Excel into a table array:
% Way 1: Simple one
MY_file = 'DATA_Exp.xlsx'; % Note the file extension
D = readtable(MY_file); % Reads all data from sheet1 if sheet1 contains any data
% Way 2: A bit more specific
% Reads a data from sheet called "DATA" and all data in cells A1 to D232
D = readtable(MY_file, sheet='DATA', Range = 'A1:D232');
% Way 3: Even more specific
% Reads a data from sheet called "DATA" and all data in cells A1 to D232,
% and preserves variable names from data table headers
D = readtable(MY_file, sheet='DATA', Range = 'A1:D232', VariableNamingRule='preserve');
%% Access Table array elements:
CELL1 = D{1,1} % Cell 1
CELL1 = 1×1 cell array
{'NSW1'}
COL2 = D{:,2} % Column 2
COL2 = 231×1 datetime array
01-Jan-2019 00:30:00 01-Jan-2019 01:00:00 01-Jan-2019 01:30:00 01-Jan-2019 02:00:00 01-Jan-2019 02:30:00 01-Jan-2019 03:00:00 01-Jan-2019 03:30:00 01-Jan-2019 04:00:00 01-Jan-2019 04:30:00 01-Jan-2019 05:00:00 01-Jan-2019 05:30:00 01-Jan-2019 06:00:00 01-Jan-2019 06:30:00 01-Jan-2019 07:00:00 01-Jan-2019 07:30:00 01-Jan-2019 08:00:00 01-Jan-2019 08:30:00 01-Jan-2019 09:00:00 01-Jan-2019 09:30:00 01-Jan-2019 10:00:00 01-Jan-2019 10:30:00 01-Jan-2019 11:00:00 01-Jan-2019 11:30:00 01-Jan-2019 12:00:00 01-Jan-2019 12:30:00 01-Jan-2019 13:00:00 01-Jan-2019 13:30:00 01-Jan-2019 14:00:00 01-Jan-2019 14:30:00 01-Jan-2019 15:00:00
COL34 = D{:,3:4} % Column 3 and 4
COL34 = 231×2
1.0e+04 * 0.7458 0.0067 0.7243 0.0069 0.6919 0.0073 0.6677 0.0070 0.6513 0.0067 0.6400 0.0064 0.6317 0.0062 0.6242 0.0050 0.6229 0.0054 0.6198 0.0050
% Alt Ways:
Cell01 = D.Reg(1) % Cell 1
Cell01 = 1×1 cell array
{'NSW1'}
Col02 = D.Time_Day(:) % Column 2
Col02 = 231×1 datetime array
01-Jan-2019 00:30:00 01-Jan-2019 01:00:00 01-Jan-2019 01:30:00 01-Jan-2019 02:00:00 01-Jan-2019 02:30:00 01-Jan-2019 03:00:00 01-Jan-2019 03:30:00 01-Jan-2019 04:00:00 01-Jan-2019 04:30:00 01-Jan-2019 05:00:00 01-Jan-2019 05:30:00 01-Jan-2019 06:00:00 01-Jan-2019 06:30:00 01-Jan-2019 07:00:00 01-Jan-2019 07:30:00 01-Jan-2019 08:00:00 01-Jan-2019 08:30:00 01-Jan-2019 09:00:00 01-Jan-2019 09:30:00 01-Jan-2019 10:00:00 01-Jan-2019 10:30:00 01-Jan-2019 11:00:00 01-Jan-2019 11:30:00 01-Jan-2019 12:00:00 01-Jan-2019 12:30:00 01-Jan-2019 13:00:00 01-Jan-2019 13:30:00 01-Jan-2019 14:00:00 01-Jan-2019 14:30:00 01-Jan-2019 15:00:00
Col034 = [D.Total, D.RRP] % Column3 and 4
Col034 = 231×2
1.0e+04 * 0.7458 0.0067 0.7243 0.0069 0.6919 0.0073 0.6677 0.0070 0.6513 0.0067 0.6400 0.0064 0.6317 0.0062 0.6242 0.0050 0.6229 0.0054 0.6198 0.0050
  댓글 수: 1
Alex K
Alex K 2023년 5월 9일
편집: Alex K 2023년 5월 9일
Thanks for the detailed answer.
But the standard syntax when working from a function does not work for me.
See my example...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by