Unexpected MATLAB Operator ':'

4 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
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)

Community Treasure Hunt

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

Start Hunting!

Translated by