MATLAB get filed values of several structs summarised in an array - matlab

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};

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'));

Updating the selected dropbox item in a GUI

Currently I have a GUI in which once a 'Submit' button is pressed the drop menu which is left blank is then populated by a calculated value determined by the three other values.
I have successfully figured out how to grab all of the values using this logic:
temp=get(handles.FSTOPpopmenu,{'String','Value'});
fstop=temp{1}{temp{2}};
if (strcmp(fstop,'Select'))
fstop = 0;
else
fstop = str2num(fstop);
end
I just have two questions about this that I can not seem to find an answer for.
How would I go about updating the 'empty' drop menu to the calculated variable (the calculated variable will already be one of the possible values in the predetermined list)?
How would I go about presenting an error, say if I have an if statement checking that the amount of zeros in the array? Would a pop up box be sufficient?
Cheers.
As for your first question matlab's set command is what you're looking for. The documentation is here. You would probably need:
MyValueIndex = find(DropDownValues==NewValue);
switch handleToChange
case handles.handle1
set(handles.handle1,'Value',MyValueIndex);
case handles.handle2
set(handles.handle2,'Value',MyValueIndex);
otherwise
error('Uh oh!');
end
Note that MyValueIndex is the index of dropdown box values that you want. Which is found with a find command on the actual value.
Question two is more of an opinion question but I think that a pop-up box describing the problem is sufficient. Maybe add in a system beep for good measure!
Reference:
http://www.mathworks.com/matlabcentral/answers/22734-resetting-a-pop-up-menu-in-a-gui
http://www.mathworks.com/help/matlab/ref/find.html
http://www.mathworks.com/help/matlab/ref/switch.html
For a popupmenu uicontrol the Value property determines which element of the String property is currently being displayed. Get the String; compare its contents to the calculated variable to get the index; then set that index as the Value property. If the calculated variable is not currently in the String then add it to the String and then set the Value. (Note when comparing you need to compare numbers to numbers or string to strings, so you may need to do an appropriate data type conversion first).
Use an errordlg.

Access multiple cells within cell array

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}].

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.