whats wrong with this code?

I have a while loop as shown in the attached file and all these variables
nr_zones_analyzed,
combinations_of_salts, vekt_prosent_best_salt_1,
vekt_prosent_best_salt_2,
samlet_vannaktivitet
,Osmotic_pressure
Are set within the loop
The code relevant for the error that occurred is included in the file.
I get the following error saying:
Conversion to double from cell is not possible.
The error points to this line:
Osmotisk_data(nr_zones_analyzed,:)={nr_zones_analyzed, combinations_of_salts, vekt_prosent_best_salt_1,vekt_prosent_best_salt_2, samlet_vannaktivitet,Osmotic_pressure}
And it occurs when I have run the code for antall_soner = 2.
Can anybody tell me whats wrong with this code..? :)

Réponses (2)

Image Analyst
Image Analyst le 30 Août 2022

0 votes

I didn't look at your code but from the sounds of it you're probably trying to do this with your cell array
dbl = str2double(ca(1));
when you should be using braces to get the contents of the cell, like this:
dlb = str2double(ca{1});
See the FAQ:
It will tell you when to use parentheses, braces, or brackets.

4 commentaires

Muazma Ali
Muazma Ali le 30 Août 2022
@Image Analyst I am sorry but I really cant see where I did such thing , can you please elaborate by looking at my code..? :)
Image Analyst
Image Analyst le 30 Août 2022
Modifié(e) : Image Analyst le 30 Août 2022
Give me code that runs
Unrecognized function or variable 'beregn_osmotisk_trykk'.
Also it asks for many inputs. What are the values you entered?
Muazma Ali
Muazma Ali le 30 Août 2022
@Image Analyst you can overlook it as I am doing a little easier for the reader and having it as an input in the file attached..
Image Analyst
Image Analyst le 30 Août 2022
No I cannot overlook (ignore) the fact that the "beregn_osmotisk_trykk" function is missing. Your code won't run without it. You must give it to us if you want our help.
Also I think you overlooked my request for the inputs that should be entered, because you did not tell us any of them. What numbers do we tell your program?

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 30 Août 2022

0 votes

varType=["double", "double", "double", "double", "double", "string"];
varNames=["Zone_nr", "Combinations_of_salts", "Weight_percent_best_salt_1", "Weight_percent_best_salt_2", "Total_water_activity", "Osmotic_pressure"];
So Combination_of_salts must be a double.
C(nr_zones_analyzed, 1)=string(best_salt_1)+ ' ' + string(best_salt_2);
Entries in C are string
combinations_of_salts=C;
So combinations_of_salts is string array.
Osmotisk_data(nr_zones_analyzed,:)={nr_zones_analyzed, combinations_of_salts, vekt_prosent_best_salt_1,vekt_prosent_best_salt_2, samlet_vannaktivitet,Osmotic_pressure}
combinations_of_salts is being sent to the second column of the table, and the second column of the table must be double, but the source is string array.

5 commentaires

Muazma Ali
Muazma Ali le 30 Août 2022
@Walter Roberson Hi ! Thanks I just edited the code but then I got a strange error message saying:
'' Element 1 must be convertible to a string
scalar.''
I am attaching the edited file
C(nr_zones_analyzed, 1)=string(best_salt_1)+ ' ' + string(best_salt_2);
each iteration of the loop, you are expanding C.
combinations_of_salts=C;
That includes all current entries in C. Second iteration of the loop it would be a 2 x 1 string array for example.
Osmotisk_data(nr_zones_analyzed,:)={nr_zones_analyzed, combinations_of_salts, vekt_prosent_best_salt_1,vekt_prosent_best_salt_2, samlet_vannaktivitet,Osmotic_pressure}
You try to import that multi-entry combinations_of_salts as a string scalar into the table.
What you should be doing is
combinations_of_salts = C(nr_zones_analyzed,1);
Muazma Ali
Muazma Ali le 30 Août 2022
Modifié(e) : Muazma Ali le 30 Août 2022
@Walter Roberson thanks a lot !
I just want to be sure that I understood it correctly ; since it is just one colum you chose to write it this way..?
The first iteration, your combinations_of_salts=C; would be trying to store one string element in the second column, and that would succeed.
The second iteration, your combinations_of_salts=C would be referring to the C that had grown to two rows of strings, so
Osmotisk_data(nr_zones_analyzed,:)={nr_zones_analyzed, combinations_of_salts, vekt_prosent_best_salt_1,vekt_prosent_best_salt_2, samlet_vannaktivitet,Osmotic_pressure}
would be trying to store a string array with two rows into the one table output row. But when you specify "string" for the variable type, that expects exactly one string per row, not a string array with two or more rows, so it fails.
You do not want to be storing the entire accumulated C array as you go. You do not want
1 ... "abc" ...
2 ... ["abc"; "def"] ...
3 ... {"abc"; "def"; "ghi"] ...
You just want
1 ... "abc" ...
2 ... "def" ...
3 ... "ghi" ...
So your combinations_of_salts should, at any time, only reflect the new information to be added to the table, not all of the accumulated information.
Muazma Ali
Muazma Ali le 23 Sep 2022
@Walter Roberson It worked for some time then I got same error message when I ran my own program.
And if I run this file attached which is a shorter version of my prog, I dont get anything out for combinations of salts field.. :(
Please help me with my problem

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Question posée :

le 30 Août 2022

Commenté :

le 23 Sep 2022

Community Treasure Hunt

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

Start Hunting!

Translated by