How to merge two characters into one in Matlab Simulink
2 views (last 30 days)
Show older comments
Muhammad Amir Khan
on 21 Sep 2023
Commented: Muhammad Amir Khan
on 22 Sep 2023
Hello,
I am new and i am struggling with merging two inputs into one after some conversion process. It was easy at normal matlab .m file execution, while i am struggling with the Simulink as it doesn't take "num2str" command and giving error. Can anyone guide me how to merge two values. I will share the .m file code with you guys just for reference.
R(i,1:68)=read(device_T,68,"uint8");
Time(i,1)=(i*delay);
%RC_ROLL
RC_ROLL_LSB= dec2hex(R(i,26)); %26 25
RC_ROLL_MSB= dec2hex(R(i,25));
RC_ROLL_Comb= strcat(num2str(RC_ROLL_LSB),num2str(RC_ROLL_MSB));
RC_ROLL(i,1)=hex2dec(RC_ROLL_Comb);
this read the serial and save require data accordinly to it for later use and graph plotting. This is the simulink function code:
function RC_ROLL = fcn(R)
%RC_ROLL
RC_ROLL_LSB= R(26); %26 25
RC_ROLL_MSB= R(25);
RC_ROLL_Comb= strcat(num2str(RC_ROLL_LSB),num2str(RC_ROLL_MSB));
RC_ROLL=hex2dec(RC_ROLL_Comb);
and this is the model picture:
and this is the error:
num2str is supported only for constant inputs. Consider using sprintf instead. Function 'MATLAB Function' (#30.104.124), line 5, column 22: "num2str(RC_ROLL_LSB)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'RC_ROLL_Comb'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#30.164.176), line 6, column 17: "RC_ROLL_Comb" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'Modelll/MATLAB Function'
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'Modelll/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Thank you in advance.
0 Comments
Accepted Answer
dpb
on 21 Sep 2023
Edited: dpb
on 21 Sep 2023
function RC_ROLL = fcn(R)
RC_ROLL=double(256*uint16(R(26))+uint16(R(25)));
end
Of course, that's what your m-file version should do, also...there's no need for the overhead to turn back into string and then reconvert to numeric so that Simulink doesn't allow num2str() is no issue.
Of course, as the hint in the error message says, you can do the same thing as num2str() with sprintf(), just have to use a specific format string instead of letting it default -- but here there's no need for either.
3 Comments
dpb
on 21 Sep 2023
Edited: dpb
on 21 Sep 2023
Yeah, you're correct Walter, forgot about that while fixing up original...the unit8 will saturate.
Made change/fixup in Answer...that would return a uint16() result; the original using hex2dec() would return a double so probably should cast there, too.
I'm not sure if Simulink would transparently handle that or not, never had access to it...
More Answers (0)
See Also
Categories
Find more on Simulink Functions 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!