Getting this error : Operator '==' is not supported for operands of type 'cell'. what should I do?
73 views (last 30 days)
Show older comments
function [ output_array ] = Klammer_Koordinate_Trennung( input_str )
for i= 1:length(input_str)
if input_str(i) == '('
index_1 = i;
end
if input_str(i) == ','
index_2 = i;
end
if input_str(i) == ')'
index_3 = i;
break;
end
end
output_array = [str2num(input_str(index_1 + 1 :index_2 - 1)),...
str2num(input_str(index_2 + 1 : index_3 - 1 ))];
end
0 Comments
Answers (3)
Dirk Engel
on 12 Jul 2023
Your function is designed to work when the input argument is a char vector, e.g. '(1,2)', but the error indicates that your function instead gets called with a cell array, e.g., {'(1,2)'}.
If the cell array contains a single char vector, you could add the following line to the top of your function to extract the char vector from the cell:
if iscell(input_str)
input_str = input_str{1}
end
If the cell arrray contains multiple elements, consider a second outer loop over the elements of the cell array.
Note that there are builtin ways to extract numbers from formatted text, such as sscanf() or textscan(). E.g.,
sscanf('(1.1,2.2)', '(%f,%f)', [1, 2])
extracts [1.1, 2.2] from the formatted string.
0 Comments
Michael Hubatka
on 12 Jul 2023
Use regular expressions, for example
str2double(regexp("(1, 2)", "\d+", "match"))
for integer only and
str2double(regexp("(1.2, 3)", "\d+\.?\d*", "match"))
for floating point and integers.
0 Comments
See Also
Categories
Find more on Characters and Strings 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!