store 2 fields of a structure in a loop

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
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?
Not sure if I got your question...
allfitdist() takes each column of the input data, perfroms the claculations and goes to the next column.
The attached photo shows the output of the function. I want to store stats.DistName and stats.BIC for each input column. I used this line but doesnt work...
for i=1:15
stats=allfitdist(Matlab_Sl(:,i),'PDF');
data(1:16,[2*i-1,2*i]) = [stats.DistName,stats.BIC];
end
Bob Thompson
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
Tala Hed el 27 de Feb. de 2019
I used your last suggestion and here is the error:
Unable to perform assignment because the size of the left side is 1-by-2 and the size of the right side is 1-by-197.
What I am looking for is the attached photo.
The first column of the raw data (sample.mat) goes into allfitdist(). The function performs the calculation and generates a structure with 11 fields. I want to store stats.DistName and stats.BIC in the first two columns of new variable (shown in yellow in the attached photo).
Then, the socond column is the input of allfitdist(). stats.DistName and stats.BIC are stored in 3rf and 4th columns of new variable. (shown in green in the attached photo). so on till the last column of the data (15th) does into allfitdist() and stores in 19th and 30th columns of data.
desire.JPG
Please let me know if I am not clear
Thanks
Bob Thompson
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
Tala Hed el 27 de Feb. de 2019
I did try this. tried again...error
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Bob Thompson
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
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
Tala Hed el 27 de Feb. de 2019
Never thought this would become such a pain! haha
still doesnt work...
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
I replaced " {" with "[ ". Still doesnt work...
PLUS, I need all 16 rows of stats.DistName & stats.BIC. why you have 1 as the first argument only?
Thanks
Tala Hed
Tala Hed el 27 de Feb. de 2019
THanks Walter for editing your last response, but I still dont get what I want...
I want all 16 rows of stats.DistName & stats.BIC not only 1.
In addition, here is the error egain!
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Bob Thompson
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
Tala Hed el 27 de Feb. de 2019
firstly, Thanks for your time.
I rephrase what I have already asked above...if you run this you would see the errors. I attach all the codes and data again!
1) read column one of the raw data (sample.mat)
2) perform allfitdist() on that. The result is a structure. (picture below)
out.JPG
3) now I want a new variable that stores stats.DistName & stats.BIC in the first 2 coumns respectively
4) done with the first column of my raw data. Now take the second column as the input of allfitdist()
5) store stats.DistName & stats.BIC of the second coumn of the raw data in coumns 3 and 4 of the variable. as shown in the picture below:
desire.jpeg
keep doing this till column 15 of the raw data!
Hope its clear!
Walter Roberson
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
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
Tala Hed el 28 de Feb. de 2019
No particular reason, just more vissually appealing...
I cannot find the "accept" button to press tho !
Thanks for your time

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Preguntada:

el 27 de Feb. de 2019

Comentada:

el 28 de Feb. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by