Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
Tiger
Posts:
43
Registered:
6/22/12
|
|
Pass Strings to Level2 MS Function
Posted:
Mar 18, 2013 9:23 AM
|
|
Hello,
I have a Constant block with the following value: uint8('120percent.txt'). I pass this value as an input to my Level2 MS Function. I've set the associated Inputport.Dimensions to be dynamic i.e. adapting to the size of the string.
At the Outputs method, I made a local variable that reverses the InputPort.Data back to its string value using the following command: FileName = char(block.InputPort(1).Data);
When I run my file, I receive an error saying: "the file cannot be found or permission denid," and it points out at the line code which says: text_lines = length(dataread('file', FileName, '%s', 'delimiter', '\n'));
Although I have, in this Level2 Function, used FileName before declaring text_lines when I declared the 'fid' variable as the following: fid = fopen(FileName); and it didn't complain at that line, but it went further down my code and the error message pointed out at the 'text_lines' code line.
Any reasons why the 'length' and/or 'dataread' functions aren't wlecoming the char() function inside it, or why am I having that error in the first place.
Here's my code for your observations: __________________________________________________ function Level2_WF_iv(block) setup(block);
function setup(block) % Register number of ports. block.NumInputPorts = 2; % d from the Entity Gen. block. block.NumOutputPorts = 10;
% Setup port properties to be dynamically inherited. block.SetPreCompInpPortInfoToDynamic; block.SetPreCompOutPortInfoToDynamic;
% Override input port properties. % d; used for indexing block.InputPort(1).DimensionsMode = 'Fixed'; block.InputPort(1).Dimensions = 1; block.InputPort(1).DatatypeID = 0; % double block.InputPort(1).Complexity = 'Real'; block.InputPort(1).SamplingMode = 'Sample'; block.InputPort(1).DirectFeedthrough = true; % FileName block.InputPort(2).DimensionsMode = 'Fixed'; block.InputPort(2).Dimensions = -1; block.InputPort(2).DatatypeID = 3; % uint8 block.InputPort(2).Complexity = 'Real'; block.InputPort(2).SamplingMode = 'Sample'; block.InputPort(2).DirectFeedthrough = true;
% Override output port properties. ...
block.SampleTimes = [-1 0]; block.SimStateCompliance = 'DefaultSimState';
block.RegBlockMethod('SetInputPortDimensions', @SetInpPortDims); block.RegBlockMethod('Outputs', @Outputs);
function SetInpPortDims(Block, idx, di) Block.InputPort(idx).Dimensions = di;
function Outputs(block) fclose('all') FileName = char(block.InputPort(1).Data); This is where I reverse back the Input from 'uint8' into a 'string' % FileName = '120percent.txt'; fid = fopen(FileName); if block.InputPort(1).Data < 1 block.InputPort(1).Data = 1; end text_lines = length(dataread('file', FileName, '%s', 'delimiter', '\n')); if block.InputPort(1).Data <= text_lines a = textscan(fid, '%f%f%f%f%f%f%f%s', 1, 'delimiter', '\t', 'headerlines', block.InputPort(1).Data-1); % 'emptyvalue', NaN('double') block.OutputPort(1).Data = a{1}; % J_ID block.OutputPort(2).Data = a{2}; % T_ID block.OutputPort(3).Data = a{3}; % NoT block.OutputPort(4).Data = a{4}; % Cores block.OutputPort(5).Data = a{5}; % T_ET block.OutputPort(6).Data = a{6}; % J_ET Deps = a{8}; % Raw values with (\) between Deps. num_slash = length(findstr(mat2str(cell2mat(Deps)), '/')); extract = sscanf(cell2mat(Deps), '%d/')'; len_num = length(extract); len_nan = num_slash - len_num + 1; block.OutputPort(7).Data(1:len_num) = extract; block.OutputPort(7).Data(len_num+1:len_num+len_nan) = NaN('double'); block.OutputPort(8).Data = a{7} + (a{5} * 1.2); % T_Deadline = RT + (T_ET * 1.2) block.OutputPort(9).Data = a{7}; % Release Time block.OutputPort(10).Data = a{7} + (a{6} * 1.2); % J_Deadline = RT + (J_ET * 1.2) fclose(fid) fclose('all') else block.OutputPort(2).Data = NaN('double'); end __________________________________________________
|
|
|
|