I have a cell of dimension 100x1 in matlab. I want to search this cell for a particular string so that it returns me the indexes where the value is that particular string. How can I accomplish this?
With strfind().
For example,
strfind({'aa','bb','cc'},'aa')
Related
I'd like to understand why the following code works:
close all
clear all
t=[0:0.1:10];
x=figure(1);
plot(t,t.^2)
a=getframe(gcf);
b{1}=frame2im(a);
instead the following code does not work:
close all
clear all
t=[0:0.1:10];
x=figure(1);
plot(t,t.^2)
a=getframe(gcf);
b(1)=frame2im(a);
If I use "b(1)=x;" it works.
Thank you very much.
In an array, you can store only one 1x1 value of any class at a single index but the class of all elements of an array must be same. In a cell array, there is no such restriction.
frame2im(a) is [525x700x3 uint8] and hence you can store it in a cell and not in a simple array if you want to store it at a single index.
b(1)=x; works because x is 1x1 matlab.ui.Figure. You can also store x in a cell array.
To my understanding, you need to know what cells are meant for in MATLAB. If you happen to know Python, you probably will think in a "list"-type way. MATLAB cell can store numbers, strings, etc. However its array is meant to store numbers.
That's why your structure from fram2im can't work.
I have created a function which takes vectors for input variables and returns a cell array for each set of inputs. The final output variable (out) seems to consist of a 2x1 cell containing two 1x5 cells. I have provided a screenshot of this below:
I am just trying to figure out how to flatten the cell array (out) to be a 2x5 cell array.
One way to achieve that would be -
vertcat(cell_array1{:})
If your cell has unequal number of elements in each row , maybe this might work better
vector=[cell_array{:}]
I've made a cell array that goes through the names of 50 text files I need to analyze. However, when referencing a particular character element as the title for textread, I get an error.
Example:
NameCell = {'file1.txt' 'file2.txt'};
vec = textread(NameCell{1},'%n','headerlines',23);
If I input the actual file name ('file1.txt') into the textread function, I get the vector I want, but it can't reference from the cell. Is there a way around this?
Thank you!
I have a cell array of 447*1 Dimensions. The cell array has 2Dimensional arrays of different dimensions of type double. I want to check a particular value in that cell array compare and on that basis store it in a new Matrix.
So for example my my starting cell array is Y{447*1} . My first cell contains an array of
5*10 and second array contains data of 22*10 . I want to evaluate the second column
of this array and then store it in a new Matrix.
I did this for one set of data and the code looks something like this.
A = [y{2,1}(1:20,2),y{4,1}(1:20,2),y{6,1}(1:20,2),y{8,1}(1:20,2),...
y{10,1}(1:20,2),y{12,1}(1:20,2),y{14,1}(1:20,2),y{16,1}(1:20,2),...
y{18,1}(1:20,2),y{20,1}(1:20,2),y{22,1}(1:20,2),y{24,1}(1:20,2),...
y{26,1}(1:20,2),y{28,1}(1:20,2),y{30,1}(1:20,2)];
But I want to automate the thing. Please help how this can be done.
Something along the lines of:
Temp = cellfun(#(x) x(1:20,2),Y(1:2:end,1), 'UniformOutput', false);
A = cat(2,Temp{:});
Should work if I am reading your question right - it should replicate your example anyway.
You can then change the dimensions of the #(x) function x(1:20,2) to take out different values from your cell array, and use different cell indexing for Y(:,1) to pick different parts of Y.
If I have a Matlab CELL line of string characters separated by a comma like:
12/28/2012,00:00:01,0.99458,1,10518,0.99458,0.99483,0,0,0,0,-,-,-,b,-,C
How do I separate each one into its own column within a matrix? What is the most efficient way of doing this?
Can this be done?
Thanks
UPDATE: Just realized this question duplicates how-to-convert-comma-separated-string-to-cell-array-of-strings-in-matlab and that my answer duplicates the answer provided by #Jonas. I'm flagging it as a duplicate.
Here is the most efficient method I'm aware of:
%# Build your string
Input = '12/28/2012,00:00:01,0.99458,1,10518,0.99458,0.99483,0,0,0,0,-,-,-,b,-,C';
%# Convert to cell array of strings
Output = regexp(Input, '([^,]*)', 'tokens');
Output = cat(2, Output{:});
Some points to note:
1) Note, this will work whether Input is a character array (ie a string) or a cell containing a character array.
2) I've assumed you want the output to be a cell array of strings. Offhand, I can't think of another way of grouping strings of differing length into separate columns using Matlab without building your own custom class.
3) The solution provided by #IlyaKobelevskiy will group the strings into separate rows (not columns) and thus essentially limits you to only a single observation.
Assuming str is your cell line:
str ={'12/28/2012,00:00:01,0.99458,1,10518,0.99458,0.99483,0,0,0,0,-,-,-,b,-,C'};
ind=strfind(str{1},',');
len=diff([0 ind]);
sz=max(len);
A=zeros(length(ind),sz);
prev=1;
for i=1:length(ind)
A(i,1:len(i)-1)=str{1}(prev:(ind(i)-1));
prev=ind(i)+1;
end;
A=char(A);
Not sure if it is the most efficient way, but should work...