loop through field in a matlab struct array - matlab

I have a MatLab struct array as follow:
'country.source.scenario.category.entity= year'
I would like to loop over the existing 'country.source.scenario' combinations and produce cell or matrix containing the fields: category, entity and year.
Anyone as an idea about how to do that? Thanks

You can use fieldnames to get the fields at each level, and isstruct at each sublevel to see if you need to keep drilling, e.g.
fields = fieldnames(str);
for field = fields'
sub = str.(field{1});
if isstruct(sub)
%loop through fieldnames
end
end
You can put it in a recursive function whereby inside the if you again call the function. I did not do it like that because it was not clear to me what you wanted as a result, it seemed like you only wanted the tags and not the values at the end.

Related

Have a list of strings I want to populate a gui with

Basically I have an excel file, and I read into one of my matlab variables a set of strings which are the headers for the columns of the data. Since this excel file is huge and I want to have the option to select which data to analyze at a given time I am hoping to take those strings/headers and have them appear on my gui with some check boxes buttons next to them so that I can pick which ones to work on ( i need the ability to select multiple data columns). This number of headers can change from file to file, so the number of list items and checkboxes will have to change on the gui based on the data file. Any ideas how to accomplish this? I can fill my gui with a bunch of text boxes and radio buttons but then i might end up with some that are empty, or end up with some that are to few, since the data file can have anywhere from 1-50 data columns each with their own header
Alternatively if anyone knows of a better way to have my user choose what data columns to act upon other than a ton of checkboxes, i'd be happy to hear it! This seemed like the most direct approach.
I tried what was suggested and get:
The code looks like:
[Data,Text] = xlsread('C:\data.csv');
a = Text(:,1);
b = strmatch('Channel Description',a);
Channels = Text(b,:);
[vals{1:numel(Channels,1)}]=deal(false);
foo= uitable('Data',vals,'ColumnEdit', true, 'RowName',Channels);
Here is an example using uitable, similar to excaza's comment but using the excel column headers as row names in the table:
[~,s,~] = xlsread('excelFile.xlsx', 1, 'A1:ZZ1');
[vals{1:numel(s),1}] = deal(false);
foo = uitable('Data', vals, 'ColumnEdit', true, 'RowName', s);
colsToLoad = foo.Data;
You will then need to either read the entire table and select columns in MATLAB using colsToLoad or loop over colsToLoad and read each column separately as xlsread does not support non-contiguous ranges.
[Data,Text] = xlsread('excelFile.xlsx');
dataToProcess = Data(:,[foo.Data{:}]);

when accessing teacher(1,2) why it doesn't execute both names khan and taj?

The MATLAB code
teacher(1)=struct('FirstName','Sohaib','LastName','Khan');
teacher(2)=struct( 'FirstName','Murtaza','LastName','Taj');
disp(teacher(1,2).LastName(1:end))
creates the output Taj. Why are not both names Khan and Taj displayed?
With teacher(1,2) you access the element in the first column and the second row of teacher. This is the entry with Taj. With LastName(1:end) you only access the name of the teacher you selected with teacher(1,2).
To display the names of both teachers, you have to specify the indexes of the teachers inside square brackets [1,2]. As disp only takes one argument, you have to create an array of names to print.
disp([teacher([1,2]).LastName])

Adding a form filter

I'm currently working on a form in Microsoft Dynamics AX.
The form consists of a grid with about 10 fields from 4 different tables.
As the form is now it returns too many values so I need to include some sort of filter, it doesn't need to be dynamic, just a static filter saying only show the lines with value X in column Y.
Has anyone here got some experience with this sort of thing? Where do I start?
I must say I'm not experienced with Microsof AX at all, I've been working with it for about a month now.
I've tried to follow this guide: How to: Add Filter Controls to a Simple List Form [AX 2012]
But I got stuck at the second part (To add a control to the custom filter group) Step 2: I dont know which type of control to chose, and ik i pick lets say a ComboBox i cant get Step 3 to work because I dont see the 'Override Methods' they mention.
Well, I usually do it this way:
In ClassDeclaration, create as many QueryBuildRanges variables as fields to filter. Let's name them Criteria1, Criteria2, etc (name them properly, please, not as here)
QueryBuildRange criteria1, criteria2;
In each Datasource you need to filter, override method Init, an add code similar to this:
super();
criteria1 = this.query().datasource(tablenum(tableofdatasource)).addQueryRange(fieldNum(fieldtofilter))
//criteria1.status(RangeStatus::locked); //optional - this way you can hide filter field to user, have it readonly for users, etc
Create a control of type StringEdit or ListBox in form to be used as filter. Change their AutoDeclaration property to Yes. Override modified() method. In it, I use to put something similar to:
super();
element.changeFilters();
In form, add method changeFilters();
range rangeFromStringControl = StringEditControlName.text(); //Put in rangeFromStringControl the string to be used as filter, as a user would write it
range rangeFromListBoxControl;
criteria1.value(rangeFromStringControl);
switch (listBoxControl.Selection())
{
case NoYesAll::All:
rangeFromListBoxControl = ''; //Empty filter string - No filter at all
break;
case NoYesAll::No:
rangeFromListBoxControl = QueryValue(NoYes::No); //Or whatever string filter value you want
break;
//Etc
}
//We have all filter strs done; let's filter for each main DataSource required with new filters
DataSource1.executeQuery();
//If there is other datasources joined to this one, it's usually no necessary to call their executeQuery;
//If there are others with filters and not joined to it, call their executeQuery()
If you need this filter to be applied when form is open, set appropiate initial values to controls, and then in form's run() method:
run();
element.changeFilters();

iReport sum of two measures

I'm having problem with JasperReports report by getting sum of two fields.
I have created so far variables that holds fields "TareWeight" variable that is casting values from double to float
new Float($F{EquipmentTareWeightKg})
"CargoWt" variable that is casting from string to float, etc...
Float.parseFloat($F{UfvFlexString03})+Float.parseFloat($F{UfvFlexString04})
So now I have two variables which I can manipulate with.
The problem starts when I wish to do a "recap" using crosstab. I have setup two groups that is done with wizard, and added another in crosstab. (please refer to the printscreen below)
And now, I have created under measures SumOf Tare that is using variable TareWt, and SumOfCargoWt that is using variable CargoWt.
I have tried to create another Measure SumOfGrossWt that would do a sum of variables TareWt + CargoWt but I have got some nonsense values.
I have tried to do another approach by creating variable GrossWt that would basically do exact the same thing as previous attempt
$V{Tare}+$V{CargoWt}
or use an variable: GrossWt
$V{Tare}+$V{CargoWt} or new Float($F{EquipmentTareWeightKg})+(Float.parseFloat($F{UfvFlexString03})+Float.parseFloat($F{UfvFlexString04}))
None of these actually worked: (Sum should be done by "Sum Of Tare"+ "Sum of CargoWt" = "Sum of GrossWt") plese refer to the second picture.
Here is a complete jrxml code/file:
jrxml file
save it as *.jrxml
Can please someone help me find a solution? how can I sum those two measure fields and get correct values?
Since CargoWt field had some null values in database, addition float value with null is equal to null, so the only value which was displayed on report are the one that had values for CargoWt, all others with null value were "ignored".

Select all inputs of a given form in Jquery

I have a form object in jquery, and I'd like to select all inputs of this form.
Let's suppose my form object is called form. If the form has an id, I can just do
var id = form.attr('id');
var inputs = $('#' + id + ' input');
If not I can check this, and then manually add a temporary id, do the selection, and remove the id (or just leave it there). But this just looks too complicated, there must be an easier way, but I'm not able to find it.
Another possible way (which I'm not able to make work) would be something like
var inputs = $('input').filter(function() {
var parents = this.parents();
return ($.inArray(form, parents) != -1);
});
but this too seems complicated (and it doesn't work as stated).
By the way, from the performance point of view, which approach would be more convenient?
http://docs.jquery.com/Traversing/find
form.find('input')
should do the trick I would think. Just in case, if you're trying to get all of the input fields to grab their current values and submit them with AJAX you can just use the .serialize method of your form:
data: form.serialize(),
As far as your performance question goes, I believe your first method is more effecient, the second will iterate over every input on the page. As of jQuery 1.4 the first method is definitely more efficient, querying based off of object IDs initially has been significantly enhanced.
Im not sure what you are trying to do here... if there are multiple forms on the page then you have to have some kind of identitfier.. a pernt, and id, a class something. If you only have a single form then its as simple as $('form input').