whats wrong with this code?
2 次查看(过去 30 天)
显示 更早的评论
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..? :)
0 个评论
回答(2 个)
Image Analyst
2022-8-30
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 个评论
Image Analyst
2022-8-30
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?
Walter Roberson
2022-8-30
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 个评论
Walter Roberson
2022-8-30
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!