I have to give ascii characters as input from simulink to stateflow and need to check whether the input matches with the existing ascii character. Can anyone help me to solve this? will be of a great help?
Example:
If I give ascii characters 'AF' as input from simulink to stateflow. It has to produce 1 as output if it matches with the existing ascii character in condition.
Simulink/Stateflow prefer numeric data. You should use an integer representation of the ASCII value (using a uint8 or uint16 data type), which will make comparison almost trivial.
Matlab does not make a clear distinction between a string with just one char and a char, and as far as I know, it is not possible to use a string type in stateflow.
Convert the input to integers then use only comparisons of integers inside the state chart.
You can use this function to convert chars to integers in Matlab:
function [ integer ] = atoi( char )
%ATOI Ascii To Integer converts char to int
%
integer = char - '0' + '0' ; %matlab seems a bit lunatic when it comes to chars
end
Related
I have a file that contains byte values 0 or 1 that are formatted without any whitespace between, like 1010111101010010010101. I want to make a [1, 0, 1, ...] vector out of those, reading one digit at a time. How can I do that? I tried using fscanf(fileId,'%c') but I get ASCII codes instead of actual values. '%d' on the other hand reads the entire file as one number.
I also tried writing to file:
fprintf(file1,'%d ',matrix); //notice the space after `%d`
and reading
fscanf(file2,'%d');
but I get a Nx1 matrix and I want to keep it as 1xN.
I could transpose it to be horizontal, but I still need to add space between digits, and I don't want to do that if possible.
You can convert easily from ascii char code to integer format as follows:
text = fscanf(fileId,'%c') - '0' ;
Note that you will also pick up end-of-line characters this way if there are any.
If you only have 0/1 in your file, using fileread will accomplish the same thing but also catches EOL characters:
text = fileread('test.txt');
text = text' - '0';
You can also read the entire file with textread:
text = textread('test.txt','%s');
text = char(text) - '0' ;
Now lines are returned in a cell array with one row per line. char then converts the cell array to a regular char array. This will not capture EOL characters but char will append blank spaces (ascii code 32) if the lines are not all equal in length.
Finally, you can also read line by line by looping and applying fgetl at each iteration until the function returns a -1.
while ~isnumeric(c)
c = fscanf(fileId,'%c')
c - '0';
end
This avoids reading EOL characters and appending blank space but you need to handle catenating the data.
I'd like to plot every 9th element from an ascii file containg a single column of 24-bit signed hex values
ex.
813457
123456
241566
etc..
The problem is, I can't get Matlab to treat values 800000-FFFFFF as negative, presumably because it's not sign extending it into 32-bits.
I thought of breaking it up into strings, then converting, but sscanf requires the '0x' to convert signed %i hex values so I'm forced to use unsigned:
C = textscan(fp, '%s') %generates 16380x1 cell (instead of normal array?!)
sscanf (C{1,1}{1,1}, '%x') %convert first ascii hex element from cell to unsigned hex
Interestingly, as a test, just doing hex2dec('FFC00000') results in a postive number, how can I force all the ascii lines in the file to be imported as an array of signed 24 or 32bit data?
To do the conversion you need, you can follow these steps:
Convert from hex string to a positive integer of double type, using herx2dec, as you've already done.
Convert that value to uint32 type.
Interpret that uint32 value as an int32 (convert data type without changing underlying data), using typecast.
Example:
>> typecast(uint32(hex2dec('FFC00000')),'int32')
ans =
-4194304
I want to read a matrix from file using the following syntax in MATLAB . This matrix is of double numbers .
readmtx(fname,nrows,ncols,precision)
Here all the inputs are quite familiar to me . But I want to know about precision . The precision of int is 'int16'. What is the precision of double number?
In this case, the documentation states:
Both binary and formatted data files can be read. If the file is binary, the precision argument is a format string recognized by fread. Repetition modifiers such as '40*char' are not supported. If the file is formatted, precision is a fscanf and sscanf-style format string of the form '%nX', where n is the number of characters within which the formatted data is found, and X is the conversion character such as 'g' or 'd'. Fortran-style double-precision output such as '0.0D00' can be read using a precision string such as '%nD', where n is the number of characters per element. This is an extension to the C-style format strings accepted by sscanf. Users unfamiliar with C should note that '%d' is preferred over '%i' for formatted integers. MATLAB syntax follows C in interpreting '%i' integers with leading zeros as octal. Formatted files with line endings need to provide the number of trailing bytes per row, which can be 1 for platforms with carriage returns or linefeed (Macintosh, UNIX®), or 2 for platforms with carriage returns and linefeeds (DOS).
In addition it is helpful to look at the table summary in the fread documentation:
I want a column vector that contains only the character A, that is,
A
A
A
A
like this. So i have tried
'A'*ones(4,1)
But in place of A, it takes on value 65. How can i get A ?
You can do it this way:
repmat('A',4,1)
Or use your approach but include char to convert back to string after the multiplication:
char('A'*ones(4,1))
Multiplication of chars with doubles gives doubles; the charis cast to double, using the corresponding ASCII value.
Just cast back to char:
char('A'*ones(4,1))
but probably Luis' answer is faster ;)
The data is like this
5.1,3.5,1.4,0.2,Iris-setosa
while I read it using this
data = fscanf(file, '%f,%f,%f,%f,%s');
and it turned out that data is an array of float rather than a combination of float and string. So how do I read this data from txt?
From the Matlab docs for fscanf:
Output Arguments A: An array. If the format includes:
Only numeric specifiers, A is numeric. ... Only character or
string specifiers (%c or %s), A is a character array. ... A
combination of numeric and character specifiers, A is numeric, of
class double. MATLAB converts each character to its numeric
equivalent. This conversion occurs even when the format explicitly
skips all numeric values (for example, a format of '%*d %s').
So your best bet is to read everything in as strings, and then convert the numeric strings to numeric values, using str2num or str2double or similar.
Alternatively, since you know there are 4 floating point values that really store a floating point value, and then the rest store the numeric ASCII values for the string, you can always split up your data and cast the part you know should be a string to char. Something like:
flt = data(1:4);
str = char(data(5:end));