matlab-struct

Create structure with field names from an array

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-11 15:44:04
问题 Below I have a snippet of code that I am using to create a structure with field names that are defined in the array 'field_names'. This seems like a very clunky way of creating the structure. Is there a better way that I can do this in one line? Perhaps there is some syntax trick to help me avoid the for loop? %array of names to create field names from field_names = ['num1', 'num2', 'num3', 'etc']; data = struct() for i = 1:length(field_names) data.field_names(i) = rand() %some random value,

How to sort structure arrays in MATLAB?

血红的双手。 提交于 2020-07-17 10:15:13
问题 I'm working with an image retrieval system using color histogram intersection in MATLAB. This method gives me the following data: a real number which represents the histogram intersection distance, and the image file name. Because they are different data types, I store them in structure array with two fields, and then I save this structure in a .mat file. Now I need to sort this structure according to the histogram intersection distance in descending order in order to retrieve the image with

Remove data from struct bigger than a certain value

∥☆過路亽.° 提交于 2019-12-24 10:58:11
问题 I have a struct, that's a <1x1 struct> , and I'm trying to edit a field in the struct based on the values. The field is called GeoDist_Actual and the struct is called GeoDist_str . The field GeoDist_Actual is a <262792x1 double> , and this is the code I was trying to use in order to get rid of the values that are greater than 1.609344e+05. i =1; for i=i:size(GeoDist_str.GeoDist_Actual) if GeoDist_str.GeoDist_Actual(i,1 > 1.609344e+05 GeoDist_str.GeoDist_Acutal(i,1) = []; end end How would I

How can I dynamically access a field of a field of a structure in MATLAB?

余生颓废 提交于 2019-12-21 16:57:33
问题 I'm interested in the general problem of accessing a field which may be buried an arbitrary number of levels deep in a containing structure. A concrete example using two levels is below. Say I have a structure toplevel , which I define from the MATLAB command line with the following: midlevel.bottomlevel = 'foo'; toplevel.midlevel = midlevel; I can access the midlevel structure by passing the field name as a string, e.g.: fieldnameToAccess = 'midlevel'; value = toplevel.(fieldnameToAccess);

MATLAB “bug” (or really weird behavior) with structs and empty cell arrays

别说谁变了你拦得住时间么 提交于 2019-12-21 11:34:09
问题 I have no idea what's going on here. I'm using R2006b. Any chance someone out there with a newer version could test to see if they get the same behavior, before I file a bug report? code: ( bug1.m ) function bug1 S = struct('nothing',{},'something',{}); add_something(S, 'boing'); % does what I expect add_something(S.something,'test'); % weird behavior end function add_something(X,str) disp('X='); disp(X); disp('str='); disp(str); end output: >> bug1 X= str= boing X= test str= ??? Input

Why is there no need to define fields of structures before assigning them?

假如想象 提交于 2019-12-20 03:16:47
问题 I am working with somebody else's code in MATLAB and it looks like he is creating structures, just by using field names without declaring them at all. Is that how it works in MATLAB, you just start using case-insensitive field names of your choice? So, for example, he has something like this: classdef Emitter properties transients=[]; end end ... some other class methods function sound=makeSound() emitterthing.transients.receivedIntensity = 100 emitterthing.transients.frequency = 500 end end

Difference between empty Matlab struct S and all elements S(:)

∥☆過路亽.° 提交于 2019-12-18 04:13:02
问题 My question is: What is the difference between S and S(:) if S is an empty struct. I believe that there is a difference because of this question: Adding a field to an empty struct Minimal illustrative example: S = struct(); %Create a struct S(1) = []; %Make it empty [S(:).a] = deal(0); %Works [S.b] = deal(0); %Gives an error The error given: A dot name structure assignment is illegal when the structure is empty. Use a subscript on the structure. 回答1: [S(:).b] = deal(0) is equivalent to [S(1

How do I organize this data into structures in MATLAB?

ぃ、小莉子 提交于 2019-12-12 06:56:49
问题 Say for n=5 , the following code gives me a plot for n randomly generated nodes. These nodes are not structures (just plotted points), but I want to assign every node a message just as I did for sink and source and keep track of the nodes identity and location. For example, if node 4 has (x,y) coordinates (.3452 , .5463) , I want to assign node 4 a msg and temp_value as well. How can I do this? Code: n = input('No. of Nodes:'); sink = [0 0]; source = [1 1]; node = rand(n,2) x = node(:,1); y =

Equality of structure objects

喜夏-厌秋 提交于 2019-12-10 18:57:50
问题 I have a tree structure in matlab like this: node = sub: [1x4 struct] where each sub is also a node. node.sub(1) = sub: [1x4 struct] etc. with leaf nodes having empty sub. Suppose I have a node and I am traversing down the tree. Is there any way to check if the node 'object' is the same as any in the tree? I am not talking about the value being the same. I want the 'object' to be the same. For eg.: mynode = tree.sub(1).sub(1); isobjectequal(mynode, tree.sub(1).sub(1)) 回答1: A struct in MATLAB