Preparing MATLAB code for code generation to C code using MATLAB Coder. Error messages.

1 view (last 30 days)
I have been writing a program which reads in all the characters from a text file. It then searches for a specific string within this character array, this is a register address. Then I want to take the following 8 characters, this is the register value, and use this in future.
The register value is a 8 character representation of a hexadecimal number. I need to convert this to a 32-bit binary number.
I understand that the read function doesn't read a specific number of characters, just keeps reading until the end of the file, hence the size of REGISTERS is [Inf:Inf]. Then the strfind function could in theory find the sub-string (address in this case) more than once. In my particular case the sub-string will only appear once, but it still has size [1x:?]. The problem with that is that I then try to reference to part of REGISTERS but it doesn't like it because the position offset is not a scalar.
How can I force these variables to behave correctly? I have tried defining them before using them, but I've had no success yet.
Thanks and here is my code:
% open file custom.txt and read all data into REGISTERS
filename = 'custom.txt';
fileID = fopen(filename,'r');
frewind(fileID);
REGISTERS = fread(fileID,[1,Inf],'char=>char');
fclose(fileID);
% Search for the register value at register address 'fa80005c'
DPASS_CONF = Get_Reg_Value_ng('fa80005c',REGISTERS);
The code for Get_Reg_Value() is as follows:
function [ REG_VAL ] = Get_Reg_Value_ng( REG_ADDR, REGISTERS )%#codegen
%%Initial Values
k = 0;
REG_VALh = char(zeros(1,8));
REG_VALd = char(zeros(1,9));
REG_VAL = char(zeros(1,32));
k = strfind(REGISTERS,REG_ADDR);
k = k_(1);
REG_VALh = REGISTERS(k+9:k+17);
REG_VALd = char(hex2dec(REG_VALh));
REG_VAL = dec2bin(REG_VALd);
Ensure that the register value is passed as 32bits:
If REG_VAL is not 32 bits long...
if length(REG_VAL) < 32
Then right justify the value in a 32 bit variable.
REG_VAL = (cat(2,zeros(1,32-length(REG_VAL)),REG_VAL));
elseif length(REG_VAL) > 32
REG_VAL = REG_VAL(1:32);
end
end
The error I get is 'Expected a scalar value. This expression has size [1x:?].' This is an error in the line 'REG_VALh = REGISTERS(k+9:k+17);' and it seems to think the 'k+9' bit is the problem. Then I get a load of errors due to REG_VALh not being defined.

Answers (1)

Chinmayi Lanka
Chinmayi Lanka on 18 Jan 2017
On attempting to generate code for your function, I did not receive the error you mentioned.
I believe that it is a typo but ensure that the indexing is correct on line 9:
>> k = k(1);
Further, on line 11, there is no need to convert the result of 'hex2dec' to char. It should be:
>> REG_VALd = hex2dec(REG_VALh);

Categories

Find more on MATLAB Coder 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!