Store string after spliting as number in array in Matlab - arrays

I'm using this code to take the entered string through text field that is contain text like this( 1,1,1,3,4,7,9,9,9 ) and then I split it depends on , to store each number in array as the result in Matlab but the problem in when I'm using str2double for temp I give error I think I'm using it at the wrong place
code:
points = get(handles.pointstxt,'String');
tmp = regexp(points,'([^ ,:]*)','tokens');
tmp
notesvector = cat(2,tmp{:})
The result appears like this:
But I want to make it like this:

points = get(handles.pointstxt,'String');
tmp = regexp(points,'([^ ,:]*)','tokens');
notesvector = cat(2,tmp{:})
c = str2double(notesvector)

Related

Pythonic way in matlab to decompose array to variables

So I want to decompose array to multiple variables.
For example,
I have 'data' array of (136,9) size which is of double type.
I want to decompose the values of data(1,:) to multiple variables something like below:
[frm_id,seq_id,xmin,ymin,w,h,temp1,temp2,temp3] = data(1,:);
In python it was straightforward, but above code gives following error in matlab:
Insufficient number of outputs from right hand side of equal sign to satisfy
assignment.
I can go with something like
frm_id = data(1,1);
seq_id = data(1,2);
%ect
But I do believe there must be matlab (more neat) way to do this operation.
Thanks!
You can use num2cell to convert the matrix to a cell array then copy contents of the cell to each variable:
C = num2cell(data,1);
[frm_id,seq_id,xmin,ymin,w,h,temp1,temp2,temp3] = C{:};
I can only suggest you to create a function like this:
function [frm_id,seq_id,xmin,ymin,w,h,temp1,temp2,temp3] = myfunction (data)
frm_id = data(:,1);
seq_id = data(:,2);
xmin = data(:,3);
ymin = data(:,4);
w = data(:,5);
h = data(:,6);
temp1 = data(:,7);
temp2 = data(:,8);
temp3 = data(:,9);
so in your main code
[frm_id,seq_id,xmin,ymin,w,h,temp1,temp2,temp3] = myfunction(data);

Converting a C char array into a Matlab String using Matlab Coder

I have some code written in Matlab that I am converting into C code using Matlab Coder.
I have written a C function that I want to call from the generated C code. This works, so far so good.
The problem is that one of the parameters to the C function is a char** used to output a string.
I accomplish this by making an opaque char* and using coder.wref to get the char**. But how do I convert the opaque char* to a Matlab string so I can return it from my function?
The code goes something like this:
function [status,out] = cfun()
buf = coder.opaque('char *', 'NULL');
len = coder.opaque('size_t ', 'NULL');
status = 0;
status = coder.ceval('_cfun', coder.wref(buf), coder.wref(len));
out = buf; % How do I convert buf into a Matlab string here??
end
_cfun will write some data to buf and write the length into len.
My current solution, which almost works, is to manually make a char array in Matlab and transfer to data. Like this:
length = cast(len, 'int32');
out = char(zeros(1,length+1));
for i = 1:(length+1)
out(i) = cast(buf(i), 'int32');
end
The C code generated from this makes no sense and loops infinitely, but I can manually update it to work. However this requires a manual update every time I rerun the code generation.
The C code looks like this:
i18 = length + 1L;
copyfrom = (int)i18;
i = 1;
while (i <= copyfrom) {
source_str->data[0] = (signed char)(int)buf;
i = 2;
}
I can update it to work:
i18 = length + 1L;
copyfrom = (int)i18;
i = 1;
while (i <= copyfrom) {
source_str->data[i-1] = (signed char)(int)(buf[i-1]);
i = i+1;
}
So, is there a way to convert the char* + length into a Matlab string? Or is there a better way of doing what I am trying to?
It's a bit wasteful, but you could allocate a MATLAB char array and memcpy the data into it:
mllen = cast(len,'int32'); % or int64
mlbuf = blanks(mllen); % allocate character vector
coder.ceval('memcpy',coder.wref(mlbuf),buf,len);
use(mlbuf);
You may want a +1 or -1 on sizes based upon whether or not len counts a NULL terminator and if you want it.
If you can somehow figure out len before the call to _cfun then you can just pass mlbuf to _cfun and forget the copy.
The generated code is indexing with 0 and producing an infinite loop because as far as Coder is concerned, buf is a 1-by-1 coder.opaque. You would get an index out of bounds error if you generated a MEX file and ran it.

Arrays containing text in MATLAB

In MATLAB, when code like this is implemented:
c = ['a','b','c','d'];
you can't really do anything with the elements. To illustrate my example:
>> c
c =
abcd
and when you do c(1,1) it returns A. But for c(2,1) it returns Index exceeds matrix dimensions.
To combat this problem, is there any way I could get around it? Or perhaps a different type of array?
You need to store each string in a different row, like so:
c = ['a';'b';'c';'d'];
What you have done above is use the [], which is the string concatenation operator. What it outputs is a single string, 'abcd', stored in c(1), which is why c(2) throws an index error.
Alternatively, you could use cell arrays :
c{1} = 'a';
c{2} = 'b';

Access structure array whose index is stored in a string

I want to get a value from a structure array by code, and I'll have the index stored in a string.
I've tried to run this code:
function M = getdata(matrix,field,varargin)
exp = [];
for i = 1:nargin-3
exp = [exp num2str(varargin{i}) ','];
end
exp = [exp num2str(varargin{nargin-2})];
M = eval('matrix(exp).(Field)');
end
However, it fails.
For example, suppose I have a structure array with 2 fields, A and B. So, I could write
MyStruct(1,1).A
A possible use would be:
M = getdata(MyStruct,A,1,1)
and I want the program to do:
M = MyStruct(1,1).A
How could I do that?
Thanks!
You can use the getfield function:
M = getfield(MyStruct, {1,1} ,'A');
Or if you wanted, say, MyStruct(1,1).A(3).B:
M = getfield(MyStruct, {1,1}, 'A', {3},'B');
For the example you give, this will suffice:
function M = getdata(matrix,field,varargin)
M = matrix(varargin{:}).(field);
which you call like
getdata(myStruct, 'A', 1,1)
which makes that function pretty useless.
But, in general, when you have indices given as strings, you can follow roughly the same approach:
%// Your indices
str = {'1', '2'};
%// convert to numbers
str = cellfun(#str2double, str, 'UniformOutput', false);
%// use them as indices into structure
M = myStruct(str{:}).(field)
And if you really insist, your call to eval is just wrong:
M = eval(['matrix(' exp ').(' field ')']);
And, as a general remark, please refrain from using exp as the name of a variable; it is also the name of a built-in function (the natural exponential function).

Referencing Structs inside a Cell Array in Matlab

I'm dealing with a Matlab data structure which is analagous to "MyCellArray" in the following example:
% Create a Struct of string values inside a Cell Array
myCellArray = cell(3,1)
myStruct1 = struct('valA','aaa111','valB','bbb111','valC','ccc111')
myStruct2 = struct('valA','aaa222','valB','bbb222','valC','ccc222')
myStruct3 = struct('valA','aaa333','valB','bbb333','valC','ccc333')
myCellArray{1} = myStruct1
myCellArray{2} = myStruct2
myCellArray{3} = myStruct3
I'd like to be able to efficiently extract some of the data into a new array:
% Extract all valA values from myCellArray
% ArrayOfValA = myCellArray(< somehow get all the valA values >)
DesiredResult = cellstr(['aaa111';'aaa222';'aaa333']) % Or something similar
I'm new to Matlab and I just can't get my head around the notation. I've tried things like:
ArrayOfValA = myCellArray{(:,1).valA} % This is incorrect notation!
The real data is over 500K lines long so I'd like to avoid for loops or other iterative functions if possible. Unfortunately I can't change the original data structure but I suppose I could take a copy and manipulate that (I tried using struct2cell but I just got into another mess!). Is it possible to do this in a fast and efficient way?
Many thanks.
The following appears to work in Octave. I assume it also works in MATLAB:
>> temp = {[myCellArray{:}].valA}
temp =
{
[1,1] = aaa111
[1,2] = aaa222
[1,3] = aaa333
}
Does
myCellAsMat = cell2mat(myCellArray);
ArrayOfValA = vertcat(myCellAsMat(:).valA);
work?
edit: or horzcat, depending on the dimension and desired output of your valA field.

Resources