Subscripting into an mxArray is not supported

hi guys i am trying to convert my matlab code to c++,in this process i am getting an error saying "Subscripting into an mxArray is not supported" in the below code
1function wordtocharacter() %#codegen
2coder.extrinsic('uigetfile');
3coder.extrinsic('strcat');
4coder.extrinsic('imread');
5coder.extrinsic('imshow');
6coder.extrinsic('im2bw');
7coder.extrinsic('fprintf');
8coder.extrinsic('sprintf');
9coder.extrinsic('fullfile');
10coder.extrinsic('imwrite');
11%coder.extrinsic('BW');
12coder.extrinsic('figure');
13coder.extrinsic('delete');
14coder.extrinsic('im2double');
15
16%clc;
17%clear all;
18%close all;
19[fn fp] = uigetfile('*.bmp');
20%mkdir(fn);
21fn=strcat(fp,fn);
22I=imread(fn);
23%fn=strcat(fn,'\');
24%BW=0;
25BW=im2bw(I,0.4);
26%figure,
27imshow(BW);
28I2=im2double(BW);
29height=0;
30width=0;
31[height, width]=size(BW);
32isStart=1;
33boundary=[];
34isIn=0;
35start=0;
36factor=0.1;
37j=1;
38for j=1:width
39 hasWhite=0;
40 for i=1+round(factor*height):round(height*(1-factor))
41 if(I2(i,j)==1)
42 if(isStart==1)
43 isStart=0;
44 start=j;
45 end
46 hasWhite=1;
47 isIn=1;
48 break;
49 end
50 end
51 if(hasWhite==0)
52 if(isIn)
53 if(j-start>height/4)
54 isStart=1;
55 isIn=0;
56 boundary=[boundary start j];
57 else
58 isStart=0;
59 start=j;
60 end
61 end
62 end
63end
64num=size(boundary)/2;
65fprintf('the character is %d \n', num(2));
66%mkdir('C:\Users\basavsagar\Desktop\segcode');
67delete('E:\seg\*.JPG');
68
69for i=1:num(2)
70 f1=I2(1:height, boundary(2*i-1):boundary(2*i));
71 figure,imshow( f1);
72
73 file_name=sprintf('%d.jpg',i); % assuming you are saving image as a .jpg
74 fullFileName = fullfile('E:\seg', file_name);
75 imwrite(f1, fullFileName); % or something like this, however you choose to save your image
76end
77 %baseFileName = sprintf('image %d.png', i);
78 %fullFileName = fullfile(output, baseFileName);
79 % imwrite('i1', fullFileName);
80 % fn=strcat(fn,'figure',num2str(i),'.bmp');
81 % imwrite(f1,fn,'bmp');
82end
these are the errors i am getting
(Line no)
1 wordtocharacter 41 Subscripting into an mxArray is not supported.
2 wordtocharacter 70 Subscripting into an mxArray is not supported.
3 wordtocharacter 71 Undefined function or variable 'f1'. The first assignment to a local variable determines its class.
4 wordtocharacter 75 Undefined function or variable 'f1'. The first assignment to a local variable determines its

 Réponse acceptée

I2 is assigned as the output of im2double which is an extrinsic call. When doing that you need to assign I2 prior to the extrinsic call to give the code generation software information about its size, type and so on:
I2 = zeros(12,14);
I2 = im2double(BW);
supposing that I2 would be 12-by-14.
This answer covers it in more detail:
And some documentation on this:

9 commentaires

What if the function returns an object ? I tried this solution for that case by constructing a structure similar to that object and initializing my variable with that structure :
coder.extrinsic('operspec');
op=StructOpspec(); % Constructing a structure with the same filds and types of operspec ;
op = operspec(plant_mdl);
but it complains "MATLAB expression 'operspec' is not of the correct class: expected 'struct', found 'opcond.OperatingSpec'
Construct a proper example of the object in question. Note that the object must be a value object and must support code generation.
operspec returns an object with the following structure : struct with fields:
Model: 'BuildingUnitModelDiscrete5x4x7'
States: [19×1 opcond.StateSpec]
Inputs: [16×1 opcond.InputSpec]
Time: 0
Outputs: [4×1 opcond.OutputSpec]
Version: 3
CustomConstrFcn: []
CustomMappingFcn: []
CustomObjFcn: []
%
As you see it's fields also contain other objects. So I make a similar structure with the same fileds and sizes. But what Matlab is complaining about is not the correct structure. It says it should be a structure not object .
If you run class(operspec(plant_model)), what does that return. Is that an object or a struct?
Similarly, what does class(StructOpspec()) return?
class(operspec(plant_mdl))
ans= 'opcond.OperatingSpec'
class(StructOpspec())
ans= 'struct'
Like I said StructOpspec() is my own function which constructs a structure similar to operspec()'s output.
Thanks for the info. StructOpspec needs to return the same type as operspec, namely opcond.OperatingSpec. Using a struc for an example output of an extrinsic function that returns an object isn't supported.
An alternative can be to move all of the code using that object to a MATLAB function that you call as an extrinsic. That function would accept and return values that are not objects thereby avoiding this class of issue.
Hi, Thanks for your support.
I'm facing a simular problem.
What if the output of the extrinsic function is not an integer or double but rather an object. How can I predefine that variable?
I'm using Bluetooth function as an extrinsic function. The output of this function is a bluethooth object and the only whay to define a bluetooth object is to call the bluetooth function itself. So my problem is that how can I define a bluetooth object so that the code generation software can
Hi, Thanks for your support.
I'm facing a simular problem.
What if the output of the extrinsic function is not an integer or double but rather an object. How can I predefine that variable?
I'm using Bluetooth function as an extrinsic function. The output of this function is a bluethooth object and the only whay to define a bluetooth object is to call the bluetooth function itself. So my problem is that how can I define a bluetooth object so that the code generation software can
Max Weigert
Max Weigert le 6 Août 2020
I'm facing a similar problem, where I can't predefine an object, as it has the same name, as the function, which has the given object as output.
Have you found a solution for it already?

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by