store 2 fields of a structure in a loop
Ältere Kommentare anzeigen
I have an array (79*15). The length of each column is different. (attached).
I want to read column 1, use that as the input of a function (attached) which output is a structure with 10 fields. Store 2 of those fields and then read the next column of my data. Can you please help me...
for i=1:15
stats=allfitdist(Matlab_Sl(:,i),'PDF');
% now create a new variable or cell
% store stats.DistName and stats.BIC in 2 first columns of the new variable and done!
% go read the second column of Matlab_Sl, do the same and store stats.DistName and stats.BIC in columns 3 and 4
% and so on to the last column
end
15 Kommentare
Bob Thompson
am 27 Feb. 2019
Storing the variables is quite easy:
data(row,[2*i-1,2*i]) = [stats.DistName,stats.BIC];
Are you running two sets of loops, one for each column, and one for each row, or does allfitdist() know what to do with the values in each row?
Tala Hed
am 27 Feb. 2019
Bob Thompson
am 27 Feb. 2019
data(1:16,[2*i-1,2*i])
This is not going to work because you're trying to put two elements over an area that contains 32 elements (16x2). If you don't need to account for the different rows of Matlab_Sl, then you can just set the row index to 1.
Alternatively if you want to have all DistName values in the first column, and BIC values in the second column you can index different rows of 'data' to represent different columns of Matlab_Sl.
data(i,[1,2]) =
If that was not the error you were running into, please post the entire error message, or describe how the results are differing from what you would expect them to look like?
Tala Hed
am 27 Feb. 2019
Bob Thompson
am 27 Feb. 2019
Bearbeitet: Bob Thompson
am 27 Feb. 2019
Ok, I think I know what you're getting at.
data(:,[2*i-1,2*i])
Try this indexing.
Keep in mind that this does require that all stats.Dist and stats.BIC results have the same number of elements.
Tala Hed
am 27 Feb. 2019
Bob Thompson
am 27 Feb. 2019
Sounds like your output arrays aren't the same size then. You can either preallocate, or you can use cells.
% Preallocation
data = nan(size(Matlab_Sl,1),size(Matlab_Sl,2)*2);
data(1:size(stats.DistName,1),[2*i-1,2*i]) = ...
% Cells
data{1,[2*i-1,2*i]} = ...
Walter Roberson
am 27 Feb. 2019
Bearbeitet: Walter Roberson
am 27 Feb. 2019
data(1,[2*i-1,2*i]) = {stats.DistName, stats.BIC};
Tala Hed
am 27 Feb. 2019
Tala Hed
am 27 Feb. 2019
Bob Thompson
am 27 Feb. 2019
What exactly is on the right side? What are stats.DistName and stats.BIC? What size are they?
Tala Hed
am 27 Feb. 2019
Walter Roberson
am 27 Feb. 2019
You are trying to store everything into data(), and some of what you are trying to store is numeric. The implication is that you want to store a numeric array with the numeric data is the columns. The problem with that is that you are trying to store distname, and distname is a variable length character vector. You cannot store both characters and numeric values in a normal MATLAB array.
You should consider using a table() object, or else use a separate object to store the distribution name and a numeric array for the numeric values.
Bob Thompson
am 27 Feb. 2019
Is there a particular reason why you don't want to keep the results in a strucuture? You could just index the results of the structure to keep all results from the different input columns and you will probably have a much easier time of things.
stats(:,i) = allfitdist(Matlab_Sl(:,i),'PDF');
If you really don't want to do this then you will need to concat the different results from all of your structure values together. I don't know off the top of my head how the indexing would work to enter the different structure elements into individual cells without a loop. Concatonating the results into one cell would just result in a cell which contains either one large array, or a structure with multiple elements but only one field.
Additionally, my previous responses where using the assumption that both DistName and BIC were arrays of doubles. Because these two fields contain different classes of data the only ways to store then in a single variable would be using cells, structures, or tables. This is another reason why I ask why you are looking to move the data out of a strucutre array.
Tala Hed
am 28 Feb. 2019
Antworten (0)
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


