Access multiple cells within cell array - matlab

for i=1:30
a{i}=rand(2,2);
end
a{[6 23]}=[] %get an error here
How do I access element 6 and 23 efficiently?

If you want to assign emptys array to the contents of those two cells, you can use the brackets ([]) and deal:
[a{[6 23]}]=deal([])
If instead you want to remove those two cells entirely, use parentheses:
a([6 23])=[]
The reason a{[6 23]}=[] gives an error is because accessing a cell array that way returns a comma-separated list of the cell contents. In other words, doing [a{[6 23]}] is like doing [a{6},a{23}].

Related

Append does more than wanted

I have a tabbar view controller with three tabs. I have implemented a global array in this tabBar controller in order to make it accessible from all the tabs. It is an array of type [[[Any]]]. I wanted to modify this array from the first tab by appending an element but the problem is that instead of just appending the element, it modifies the existing element too. Can you help me pls ? Thanks.
Here is the definition of the array :
var invoices: [[[Any]]] = []
Here is the appending :
let tabBar = tabBarController as! baseTabBarController
tabBar.invoices.append(cells)
P.S: The append is done in a view controller connected to the first view controller (child of tabbar view controller) by a segue link.
You really need to give more information to get more personalised answers.
However, your Array is a three - dimensional - array, which means that you have an array inside an array inside an array.
When performing the append Method on an array, it always adds a new element to the end of this array.
In your case: The element that is added is of type [[Any]], which is a two - dimensional - array.
NOTE: More - dimensional - arrays are a bit tricky. What your code does is appending a new two - dimensional - array at the end of your outer array, not adding a new Any to your inner array!
However, the append Method will never modify existing Elements, so please check your syntax again, since more - dimensional - arrays are tricky.

How to get `length` of Matlab GUI popup menu strings

I want to get how many strings exist in the popup menu, how can this be done? This code that I have written does not seem to work.
length(get(handles.popupMenu,'Value'))
Value is the index of the currently selected item in the menu so it will only ever be a scalar. You instead want to check the length of the String property which contains a cell array of strings (one for each item).
nOptions = numel(get(handles.popupMenu, 'String'));

Getting selected cell indices from uitable

I have a uitable with 10 columns, which I'm populating from a db.
Now I want to know when the user choose a specific row. For example if the user chooses the 3rd record, I would like to get back the value 3, so then I could access the actual information to for example open that specific record from the path.
I found online that I would need findjobj.
I also think that the method should be implemented in here:
function uitable_CellSelectionCallback(hObject, eventdata, handles)
However I found a little information about how I should proceed.
Anyone had this problem or knows how to solve it?
When calling the CellSelectionCallback you can access the Indices property, which is a 2 x 1 array containing the row and column indices of the cell you have selected.
Therefore in your callback, use something like this:
row = eventdata.Indices(1)
col = eventdata.Indices(2)
and that should get you going.
In order to avoid callbacks, you can use app.UITable.Selection in the newer versions of MATLAB where app is your app object and UITable your uitable name

MATLAB get filed values of several structs summarised in an array

I have a folder containing some images. When I use dir I get an array with an entry for each image. This entry is an struct with the fields name, date, bytes and some more...
Now I want to have an array, containing the name of x images, selected by an index array index=[2 8 15 23]. How can I do this without using loops (to improve performance)?
How can I access the field value of the structs summarised in my array getting with dir?
Using
images=dir('path_to_my_folder');
index=[2 8 15 23];
names=images(index).name;
doesn't work, this only returns the name of the last image accessed by index(end)
Thanks for help.
Your code images(index).name gives a comma-separated list of strings. When assigning a list to a variable, only the first element of the list gets assigned.
To assign all elements, you need to collect the elements of that list into a cell array, and then assign the cell array to the variable:
names = {images(index).name};

is it possible to replace images in an array dynamically

hi i am new to iphone.what i am need is to place a 20 images names in an array . if i place those images in an array whether it is possible or not to change replace the first image by last means changing the positions of images dynamically if it is not possible pls suggest in what way i can done this. pls post any sample code thank u
Use NSMutableArray. It has methods like
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
It does not matter whether your objects are strings or images or whatever. It is possible to change the objects in a mutable array.