Unexpected MATLAB Operator ':'

16 views (last 30 days)
Rhett Green
Rhett Green on 3 Jan 2022
Answered: Rhett Green on 20 Jan 2022
I have created a MATLAB Function that I want to be able to read a table that contains the name of Hardware (Column1), Instruction (Column2), and Step (Column3). Each Row represents and instruction for a piece of hardware. The table containing these instructions is in the base workspace and after loading it using evalin to a variable 'x' I want to iterate through each row so I can output the instructions for each piece of hardware accordingly. However when using the ':' operator to interact with the table I receive a "Unexpected MATLAB operator ':' " error. I can index a table using x(1,1) (row 1 column 1) but not by using x(:,1) (all contents of column 1) which worked on the same table in a MATLAB script. Here is the MATLAB script that runs and is what I want to do:
contents = evalin("base","commandSequence");
header = {'Hardware' 'Instruction' 'Step'}
contents.Properties.VariableNames = header;
disp(contents)
Step = 1;
for iRow = 1:Step:height(contents)
hardware1 = contents{iRow,'Hardware'};
hardware = string(hardware1);
switch(string(hardware))
case 'Contactor1'
Instruction = contents{iRow,"Instruction"};
Step = contents{iRow,"Step"};
case 'Contactor2'
Instruction = contents{iRow,"Instruction"};
Step = contents{iRow,"Step"};
case 'Contactor3'
Instruction = contents{iRow,"Instruction"};
Step = contents{iRow,"Step"};
case 'Voltage'
Instruction = contents{iRow,"Instruction"};
Step = contents{iRow,"Step"};
end
end
Any idea what I'm doing wrong and how I can implement something similar in a MATLAB Function block?
  3 Comments
Rhett Green
Rhett Green on 4 Jan 2022
Code inside MATLAB Function:
function [Contactor1, Contactor2, Contactor3, Voltage1] = fcn()
coder.extrinsic('evalin', 'height');
contents = evalin("base","commandSequence");
header = {'Hardware' 'Instruction' 'Step'}
contents.Properties.VariableNames = header;
disp(contents)
Step = 1;
for iRow = 1:Step:height(contents)
hardware1 = contents{iRow,'Hardware'};
hardware = string(hardware1);
switch(string(hardware))
case 'Contactor1'
Contactor1 = contents{iRow,"Instruction"};
Step = contents{iRow,"Step"};
case 'Contactor2'
Contactor2 = contents{iRow,"Instruction"};
Step = contents{iRow,"Step"};
case 'Contactor3'
Contactor3 = contents{iRow,"Instruction"};
Step = contents{iRow,"Step"};
case 'Voltage'
Voltage1 = contents{iRow,"Instruction"};
Step = contents{iRow,"Step"};
end
end
Errors:
Attempt to extract field 'Properties' from 'mxArray'. Function 'MATLAB Function' (#25.190.198), line 6, column 1: "contents" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Colon operands must be all the same type, or mixed with real scalar doubles. Function 'MATLAB Function' (#25.277.293), line 9, column 19: "height(contents)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Cell contents reference from a non-cell array object. Function 'MATLAB Function' (#25.310.335), line 10, column 17: "contents{iRow,'Hardware'}" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'hardware1'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#25.359.368), line 11, column 23: "hardware1" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'hardware'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#25.394.402), line 14, column 19: "hardware" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Cell contents reference from a non-cell array object. Function 'MATLAB Function' (#25.456.484), line 16, column 26: "contents{iRow,"Instruction"}" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Cell contents reference from a non-cell array object. Function 'MATLAB Function' (#25.505.526), line 17, column 20: "contents{iRow,"Step"}" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Cell contents reference from a non-cell array object. Function 'MATLAB Function' (#25.592.620), line 21, column 26: "contents{iRow,"Instruction"}" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Cell contents reference from a non-cell array object. Function 'MATLAB Function' (#25.641.662), line 22, column 20: "contents{iRow,"Step"}" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Cell contents reference from a non-cell array object. Function 'MATLAB Function' (#25.728.756), line 25, column 26: "contents{iRow,"Instruction"}" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Cell contents reference from a non-cell array object. Function 'MATLAB Function' (#25.777.798), line 26, column 20: "contents{iRow,"Step"}" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Cell contents reference from a non-cell array object. Function 'MATLAB Function' (#25.859.887), line 29, column 24: "contents{iRow,"Instruction"}" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Cell contents reference from a non-cell array object. Function 'MATLAB Function' (#25.908.929), line 30, column 20: "contents{iRow,"Step"}" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'CommandParsing1/MATLAB Function'
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'CommandParsing1/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'CommandParsing1/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:Simulink | Category:Block error
Rhett Green
Rhett Green on 4 Jan 2022
Also you noted that using evalin is not wise. Why is that? I initially was reading in an excel file containg the table, but someone mentioned not to read in a non changing table continously in a MATLAB Function. The whole purpose of this block is to read in a Command Sequence matrix/table/file (whatever best works with simulink in external mode), and parse out the instructions to the rest of the model. If there is some block I can simply plug into my model that can easily feed the model a matrix in external mode I could use that but all the From Workspace blocks did not work.

Sign in to comment.

Accepted Answer

Rhett Green
Rhett Green on 20 Jan 2022
It turns out the issue had something to do with the way I was inputing the file to the MATLAB Function. I was able to fix it by inputing the data as a numerical matrix parameter. I went to "Ports & Data Manager" and changed the scope of the workspace variable from "Input" to "Parameter". I was then able to pass it in by:
function [Contactor1, Contactor2, Contactor3, Voltage1] = fcn(commandSequence)
The matrix had to be numerical (commandSequence) and I was then able to perform the for loop the way I had it.

More Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!