whats wrong with this code?

1 view (last 30 days)
Muazma Ali
Muazma Ali on 30 Aug 2022
Commented: Muazma Ali on 23 Sep 2022
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..? :)

Answers (2)

Image Analyst
Image Analyst on 30 Aug 2022
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 Comments
Muazma Ali
Muazma Ali on 30 Aug 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 on 30 Aug 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?

Sign in to comment.


Walter Roberson
Walter Roberson on 30 Aug 2022
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 Comments
Walter Roberson
Walter Roberson on 30 Aug 2022
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 on 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

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!