convert number from cell to double, cell2mat not working - matlab

I think I'm losing my mind.
I have a 3 x 2 cell which looks exactly like below.
Region Code
US 1
EU 2
I then have the following code to determine the row number for the EU region.
eq_code_index = find(ismember(fund.type_des(:, 1), 'EU'));
eq_code = cell2mat(fund.type_des(eq_code_index, 2));
eq_code_index returns 3 which is correct (row headers are included in the output). So I want the value in row 3, column 2 which is 2. I then use cell2mat to convert it from a cell value to an integer however it doesn't work the value is of type char? Haven't a clue why cell2mat isn't working?
Update
Even if I do the following two lines of code below I can't get the codes into a vector, they turn into char's
codes = fund.type_des(2:end, end);
codes = cell2mat(codes)

To access a single element in a cell array, use curly braces:
fund.type_des{eq_code_index, 2};
This is generally simpler than using cell2mat(). If the contents of the cell are chars and you want an integer, you have to perform the conversion. str2num() is one of many options for this:
eq_code = str2num(fund.type_des{eq_code_index, 2});

Related

Extract a specific part of cell array values

I have a cell array with values similar to the following one
13:41:54.879
I would like to extract only 13:41 part of the given value and mitigate the rest. I tried various combinations of extractBefore() and extractAfter() but couldn't get it.
You can use a regular expression to match the pattern "digits, colon, digits":
c = {'13:41:54.879', '1:22:33.45679'};
result = regexp(c, '\d+:\d+', 'match', 'once');
gives
result =
1×2 cell array
{'13:41'} {'1:22'}

converting integer chars to numeric array in matlab

I have a char array of format a = [1.234 ; 2.345; 3.456] and I need to convert this to a numeric array in MATLAB. I have tried str2num(a) but it only seems to work on integers, as it is returning an empty vector. Here is what the data actually looks like:
Any suggestions on how to tackle this problem are appreciated!
If your character array is either of the following formats:
a = '[1.234; 2.345; 3.456]'; % 1-by-N with brackets, spaces, or semicolons
a = ['1.234'; '2.345'; '3.456']; % M-by-N
Then str2num should work as you want:
vec = str2num(a)
vec =
1.234000000000000
2.345000000000000
3.456000000000000
If it's not working then that probably means that your character array val has rows with invalid formats or characters that don't properly convert. Since the array has 3100 rows, you probably don't want to search through it by hand. One easy way to highlight where invalid rows might be is to identify where there are characters other than numbers, periods, or white space. Here's how you can get a list of rows that may warrant further inspection:
suspiciousRows = find(~all(ismember(val, '0123456789. '), 2));
The function str2double would works for your case. Please refer to this link for detailed usage.
str2num would work as it is but as str2num uses eval,so a better alternative is str2double. But it is not directly applicable on a char array like yours. You can convert that array into a cell using cellstr and then apply str2double.
req = str2double(cellstr(val))
Another approach if you have MATLAB R2016b or a later version is to convert that char array into a string array and then apply str2double.
req = str2double(string(val))

Using fscanf in MATLAB to read an unknown number of columns

I want to use fscanf for reading a text file containing 4 rows with an unknown number of columns. The newline is represented by two consecutive spaces.
It was suggested that I pass : as the sizeA parameter but it doesn't work.
How can I read in my data?
update: The file format is
String1 String2 String3
10 20 30
a b c
1 2 3
I have to fill 4 arrays, one for each row.
See if this will work for your application.
fid1=fopen('test.txt');
i=1;
check=0;
while check~=1
str=fscanf(fid1,'%s',1);
if strcmp(str,'')~=1;
string(i)={str};
end
i=i+1;
check=strcmp(str,'');
end
fclose(fid1);
X=reshape(string,[],4);
ar1=X(:,1)
ar2=X(:,2)
ar3=X(:,3)
ar4=X(:,4)
Once you have 'ar1','ar2','ar3','ar4' you can parse them however you want.
I have found a solution, i don't know if it is the only one but it works fine:
A=fscanf(fid,'%[^\n] *\n')
B=sscanf(A,'%c ')
Z=fscanf(fid,'%[^\n] *\n')
C=sscanf(Z,'%d')
....
You could use
rawText = getl(fid);
lines = regexp(thisLine,' ','split);
tokens = {};
for ix = 1:numel(lines)
tokens{end+1} = regexp(lines{ix},' ','split'};
end
This will give you a cell array of strings having the row and column shape or your original data.
To read an arbitrary line of text then break it up according the the formating information you have available. My example uses a single space character.
This uses regular expressions to define the separator. Regular expressions powerful but too complex to describe here. See the MATLAB help for regexp and regular expressions.

Function to split string in matlab and return second number

I have a string and I need two characters to be returned.
I tried with strsplit but the delimiter must be a string and I don't have any delimiters in my string. Instead, I always want to get the second number in my string. The number is always 2 digits.
Example: 001a02.jpg I use the fileparts function to delete the extension of the image (jpg), so I get this string: 001a02
The expected return value is 02
Another example: 001A43a . Return values: 43
Another one: 002A12. Return values: 12
All the filenames are in a matrix 1002x1. Maybe I can use textscan but in the second example, it gives "43a" as a result.
(Just so this question doesn't remain unanswered, here's a possible approach: )
One way to go about this uses splitting with regular expressions (MATLAB's strsplit which you mentioned):
str = '001a02.jpg';
C = strsplit(str,'[a-zA-Z.]','DelimiterType','RegularExpression');
Results in:
C =
'001' '02' ''
In older versions of MATLAB, before strsplit was introduced, similar functionality was achieved using regexp(...,'split').
If you want to learn more about regular expressions (abbreviated as "regex" or "regexp"), there are many online resources (JGI..)
In your case, if you only need to take the 5th and 6th characters from the string you could use:
D = str(5:6);
... and if you want to convert those into numbers you could use:
E = str2double(str(5:6));
If your number is always at a certain position in the string, you can simply index this position.
In the examples you gave, the number is always the 5th and 6th characters in the string.
filename = '002A12';
num = str2num(filename(5:6));
Otherwise, if the formating is more complex, you may want to use a regular expression. There is a similar question matlab - extracting numbers from (odd) string. Modifying the code found there you can do the following
all_num = regexp(filename, '\d+', 'match'); %Find all numbers in the filename
num = str2num(all_num{2}) %Convert second number from str

Understanding the fastaread function in matlab

I searched the documentation about the fastaread function, but I'm still kind of confused about the Sequence part.
So, suppose my file is stored in the file_path location, and fastaread(file_path) will return the data obtained.
fastaread returns two columns, one is with title header and the other one is with title sequence. Then, fastaread(file_path).sequence will return the sequence column? Does that mean that fastaread(file_path).sequence is a column vector?
Actually it's a row vector containing characters.
Example with data provided with Matlab:
p53nt = fastaread('p53nt.txt')
gives a structure with 2 fields: Header and Sequence.
p53nt.Header gives info about the actual sequence:
p53nt.Header
ans =
gi|8400737|ref|NM_000546.2| Homo sapiens tumor protein p53 (Li-Fraumeni syndrome) (TP53), mRNA
while p53nt.Sequence gives a character array of size 1xN:
S = p53nt.Sequence
S =
ACTTGTCATGGCGACTGTCCAGC... And so on
Typing whos S gives this:
Name Size Bytes Class Attributes
S 1x2629 5258 char
So if you want a column vector for some reason, use the colon operator:
S = S(:);
Hope its a bit clearer now!