Unexpected MATLAB Operator ':'
Show older comments
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
Image Analyst
on 3 Jan 2022
Edited: Image Analyst
on 3 Jan 2022
I don't see the colon except in the "for" line, and that looks okay. Can you give us the error message (ALL the red text) so we can see the actual line of code that threw the error. Also attach contents in a .mat file
contents = evalin("base","commandSequence"); % Get contents from the base workspace (probably not good coding practice though).
% Now save it for the Answers forum
save('answers.mat', 'contents');
Attach answers.mat with the paperclip icon.
If none of your cases are met, then Instruction and Step are not assigned and that will cause errors later. Your last case should be "otherwise" instead of "case 'Voltage'".
Rhett Green
on 4 Jan 2022
Rhett Green
on 4 Jan 2022
Accepted Answer
More Answers (0)
Categories
Find more on Class Introspection and Metadata in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!