Deleting elements from a cell in Matlab - matlab

In a matrix, to remove the columns in which the element of the first line is 0, we can use:
ind2remove = (A(1,:) == 0);
A(:,ind2remove) = [];
How do I do that if A is a cell? I want to remove the columns in which the element of the first row is 0.
I tried:
ind2remove = (A{1,:} == 0);
A{:,ind2remove} = [];
but I got the error message:
??? Error using ==> eq
Too many input arguments.
Error in ==> ind2remove = (A{1,:} == 0);

Indexing using { } gives you the contents of the cell, whereas indexing using ( ) returns the same type as the original object i.e., if A is a cell, A{i,j} will return what it's holding, and A(i,j) will always return a cell. You need the latter.
So in your case, you can do the following to eliminate all columns where the first row has a 0.
A(:, cellfun(#(x)x==0, A(1,:))) = [];
The assumption here is that each cell in the first row holds only a single numeric element, as per your comment.

Related

Return " i " value when if-statement is true

I have created a matrix with row 1 full of strings and 4 other rows with numbers. They are created in a handle class with the object "Projekter".
So in the object "Projekter" row 1, the first value is blank, but the second value is 'Ole'. So I know that 'Ole' is in (1,2). x is the name/string I want to search for, which in this case is 'Ole'.
As you see below it should search row 1 from column 2 untill the last name/string and if i = 'Ole', it should bring me the value 2 because " i " should be equal 2.
A is just a controller if the function works, but at this point it doesn't.
The error it gives is "Undefined function 'eq' for input arguments of type 'cell'."
How do I fix this so it return the " i " value when the statement is correct?
Thank you in advance!
function number(obj,x)
A = [];
for i = 2:size(obj.Projekter,2)
if obj.Projekter(1,i)==x
A = A + 1;
end
end
disp(A)
end
Maybe you have to index the cell content:
your_cell = {'a_string'};
your_string = your_cell{1};
function [returnValue] = number(obj,x)
for i = 2:size(obj.Projekter,2)
if obj.Projekter{1,i}==x
returnValue = i;
return;
end
end
end
Note the change from obj.Projekter(1,i)==x to obj.Projekter{1,i}==x (use curly braces instead of parens). I have then specified that returnValue will hold the value that should be returned by doing function [returnValue] = number(obj,x). We then set returnValue equal to i and return from the function when the condition of the if statement is true.
As suggested in the comments, it is probably better to do:
function [returnValue] = number(obj, x)
returnValue = find(strcmp(x, obj.Projekter) == 1);
strcmp(x, obj.Projektor) will give you an array the length of obj.Projekter with 1's wherever the strings match, and 0's where they don't, you can then find the indices that are set to 1. This has the added benefit of
not using a loop so it's faster
Giving you every occurrence of a match, not just the first one.

MATLAB - Cell array to a cell

I want to create a {1,2} cell array, that will contain single cell value in {1,1} and several cells in {1,2}{:,1} row:
TEXT = {'-1';'432';'a';'c';'v';'-1';'-1';'14';'b';'n';'-1';'-1';'44';'m';'t';'r';'-1';'-1';'55';'o';'i';'u';'-1'};
Sections{1,1} = TEXT{5};
Sections{1,2}{:,1} = TEXT{3:10,1};
,but I get an ERROR:
The left hand side is initialized and has an empty range of indices.
However, the right hand side returned one or more results.
Error in fff (line 4)
Sections{1,2}{:,1} = TEXT{3:10,1}; % Text in the section
Ideas?
Just use () instead of {} for the sections in (n,2):
TEXT = {'-1';'432';'a';'c';'v';'-1';'-1';'14';'b';'n';'-1';'-1';'44';'m';'t';'r';'-1';'-1';'55';'o';'i';'u';'-1'};
Sections{1,1} = TEXT{5};
Sections{1,2} = TEXT(3:10,1);

How do I iterate through an imported excel doc?

what I need to do for this code is import a giant (302x11) excel doc, and then ask the user for an input. Then, I need to iterate through each element in the 5th column of the excel array, and if that element matches the user input, save the entire row to a new array. After going through all 302 rows, I need to display the new array.
So far, I have this:
Vin = input('Vin: ');
filename='MagneticCore.xlsx';
sheet=2;
xlRange='B2:L305';
[ndata, text, alldata] = xlsread(filename,sheet,xlRange,'basic');
After this, I'm not sure how to iterate through the alldata array.
alldata is a cell, to select the fifth column you can use alldata{:,5}. Searching in Cells is done this way without iterating
Try it on your own, if you get stuck update your question with code and error message
Daniel R is right, you can do this without iterating through the cell array. Here's how you could iterate through the array if you needed to:
[ndata, text, alldata] = xlsread('Book1.xlsx');
target = 12;
newArray = {};
for r = 1:size(alldata, 1)
% get the element in the fifth column of the current row
e = raw{r,5};
if e == target
% add to newArray
newArray{end + 1} = alldata(r,:);
end
end
% display newArray
for r = 1:size(newArray, 1)
disp(newArray{r})
end

extracting the index of a certain character in a cell array

I have a cell array like this:
and I want to extract the index of 2 in this cell array so I used these lines of codes:
for i = 1:size(idx,1)
if idx{i,1} ~= []
index = i;
end
end
but the code doesn't work.I mean the debuger never enters if beacause it doesn't understand that 2 differs from [].why? and how do you suggest me to write the code?
note that the character will not always be 2 and it may occur in other indexes too.
To test if you variable is empty use ISEMPTY function.
To do it for all elements in a cell array you can use CELLFUN:
index = find(~cellfun(#isempty, idx));
In Matlab, [] means empty, thus:
for i = 1:size(idx,1)
if ~isempty(idx{i,1})
index = i;
end
end

MATLAB: Using the If statement with a string to return values from other arrays

I have a matlab program where I am importing some arrays from excel. I am trying to write an if statement that looks in the first array, say:
Thing-1 Thing-1 Thing-3 Thing-5
If a column is a "Thing-1", then it goes to a different array, and calculates 3 values that are to be given different variable names...any guidance would be much appreciated! Thanks!
You need a function like vlookup as in Excel.
I have written one. Here is the source code:
function [content, index] = vlookup(m, e, column, lookcolumn)
if isempty(m) || isempty(e), return; end
if nargin <= 3, lookcolumn = 1; end
isechar = ischar(e);
assert(isechar || isnumeric(e), 'the second parameter must be a string or numeric');
if iscell(m)
content = {}; index = [];
if isechar
index = find(strcmp(e, m(:, lookcolumn)));
content = m(index, column);
else
for i = 1:size(m, 1)
if isnumeric(m{i, lookcolumn}) && m{i, lookcolumn} == e
content = [content; m(i, column)]; %#ok<*AGROW>
index = [index; i];
end
end
end
else
assert(~isechar, 'When the first para is a matrix, the second para must be numeric');
index = find(m(:, lookcolumn) == e);
content = m(index, column);
end
The question is... not very clear, but let me try and give you some suggestions.
Say you read some data from an Excel workbook in which the first row is headers, followed by lots of rows with numbers.
[num,txt] = xlsread(excelFileName);
so that num contains the numeric data and txt the string column headers.
Then you can check for the string Thing-1 in the column headers. thingOneIdx is an array with indices into the columns of the header. In your example, it would be [1 2], since the first two columns are Thing-1.
thingOneIdx = find(strcmp('Thing-1',txt));
You can create three cell arrays, firstValue, secondValue, and thirdValue that will store the results of the three calculations. If you need to keep the Thing-1 data around in an additional array, you can do that analogously.
%# define cell arrays (do it in one go using deal)
[firstValue,secondValue,thirdValue] = deal(cell(length(thingOneIdx),1));
%# for simplicity and readability, loop through isThingOneIdx to assign data
for ct = 1:length(thingOneIdx)
myIdx = thingOneIdx(ct);
firstValue{ct} = someCalculation(num(myIdx,:));
secondValue{ct} = someOtherCalculation(num(myIdx,:));
%# etc
end