store 2 fields of a structure in a loop
Mostrar comentarios más antiguos
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 comentarios
Bob Thompson
el 27 de Feb. de 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
el 27 de Feb. de 2019
Bob Thompson
el 27 de Feb. de 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
el 27 de Feb. de 2019
Bob Thompson
el 27 de Feb. de 2019
Editada: Bob Thompson
el 27 de Feb. de 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
el 27 de Feb. de 2019
Bob Thompson
el 27 de Feb. de 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
el 27 de Feb. de 2019
Editada: Walter Roberson
el 27 de Feb. de 2019
data(1,[2*i-1,2*i]) = {stats.DistName, stats.BIC};
Tala Hed
el 27 de Feb. de 2019
Tala Hed
el 27 de Feb. de 2019
Bob Thompson
el 27 de Feb. de 2019
What exactly is on the right side? What are stats.DistName and stats.BIC? What size are they?
Tala Hed
el 27 de Feb. de 2019
Walter Roberson
el 27 de Feb. de 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
el 27 de Feb. de 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
el 28 de Feb. de 2019
Respuestas (0)
Categorías
Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


